


from heapq import heappop, heappush

import numpy as np


def     lowercase	(    a     , a     , a     , a     , ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Tuple				     =						grid.shape
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						[-1, 1, 0, 0]
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						[0, 0, -1, 1]
		if allow_diagonal:
				dx += [-1, -1, 1, 1]
				dy += [-1, 1, -1, 1]

		SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:List[str]				     =						[(0, source)], set()
		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						np.full((rows, cols)     , np.inf )
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						0
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						np.empty((rows, cols)     , dtype=a )
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						None

		while queue:
				((SCREAMING_SNAKE_CASE_)     ,				(SCREAMING_SNAKE_CASE_))				:Union[str, Any]				     =						heappop(a )
				if (x, y) in visited:
						continue
				visited.add((x, y) )

				if (x, y) == destination:
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						[]
						while (x, y) != source:
								path.append((x, y) )
								SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						predecessors[x, y]
						path.append(a )  # add the source manually
						path.reverse()
						return matrix[destination], path

				for i in range(len(a ) ):
						SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Any				     =						x + dx[i], y + dy[i]
						if 0 <= nx < rows and 0 <= ny < cols:
								SCREAMING_SNAKE_CASE_				:Optional[int]				     =						grid[nx][ny]
								if next_node == 1 and matrix[nx, ny] > dist + 1:
										heappush(a     , (dist + 1, (nx, ny)) )
										SCREAMING_SNAKE_CASE_				:List[Any]				     =						dist + 1
										SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						(x, y)

		return np.inf, []


if __name__ == "__main__":
		import doctest

		doctest.testmod()










def     lowercase	(    a     , a     , a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:int				     =						[False] * len(a )
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						[]
		queue.append(a )
		SCREAMING_SNAKE_CASE_				:int				     =						True

		while queue:
				SCREAMING_SNAKE_CASE_				:int				     =						queue.pop(0 )
				for ind in range(len(graph[u] ) ):
						if visited[ind] is False and graph[u][ind] > 0:
								queue.append(a )
								SCREAMING_SNAKE_CASE_				:Tuple				     =						True
								SCREAMING_SNAKE_CASE_				:Optional[int]				     =						u

		return visited[t]


def     lowercase	(    a     , a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Any				     =						[-1] * (len(a ))
		SCREAMING_SNAKE_CASE_				:Tuple				     =						0
		while bfs(a     , a     , a     , a ):
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						float("Inf" )
				SCREAMING_SNAKE_CASE_				:str				     =						sink

				while s != source:
						# Find the minimum value in select path
						SCREAMING_SNAKE_CASE_				:str				     =						min(a     , graph[parent[s]][s] )
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						parent[s]

				max_flow += path_flow
				SCREAMING_SNAKE_CASE_				:Dict				     =						sink

				while v != source:
						SCREAMING_SNAKE_CASE_				:int				     =						parent[v]
						graph[u][v] -= path_flow
						graph[v][u] += path_flow
						SCREAMING_SNAKE_CASE_				:Any				     =						parent[v]
		return max_flow


SCREAMING_SNAKE_CASE__     					=			[
    [0, 16, 13, 0, 0, 0],
    [0, 0, 10, 12, 0, 0],
    [0, 4, 0, 0, 14, 0],
    [0, 0, 9, 0, 0, 20],
    [0, 0, 0, 7, 0, 4],
    [0, 0, 0, 0, 0, 0],
]

SCREAMING_SNAKE_CASE__					,    SCREAMING_SNAKE_CASE__     					=			0, 5
print(ford_fulkerson(graph, source, sink))






