Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Maximum Flow

A flow network is a directed graph which has a $source$ and a $sink$. In a flow network, each edge $(u, v)$ has a capacity $c(u, v)$. Each edge receives a flow, but the amount of flow on the edge can not exceed the corresponding capacity. Find the maximum flow from the $source$ to the $sink$.

Input

A flow network is given in the following format.

$|V|\;|E|$

$u_0\;v_0\;c_0$

$u_1\;v_1\;c_1$

:

$u_{|E|-1}\;v_{|E|-1}\;c_{|E|-1}$

$|V|$, $|E|$ is the number of vertices and edges of the flow network respectively. The vertices in $G$ are named with the numbers 0, 1,..., $|V|-1$. The source is 0 and the sink is $|V|-1$.

$u_i$, $v_i$, $c_i$ represent $i$-th edge of the flow network.
A pair of $u_i$ and $v_i$ denotes that there is an edge from $u_i$ to $v_i$ and $c_i$ is the capacity of $i$-th edge.

Output

Print the maximum flow.

Constraints

$2 \leq |V| \leq 100$

$1 \leq |E| \leq 1000$

$0 \leq c_i \leq 10000$

Sample Input 1

4 5
0 1 2
0 2 1
1 2 1
1 3 1
2 3 2

Sample Output 1

3

Solution in JAVA (judge verdict: Accepted)

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Main m = new Main();
		m.run();
	}

	Scanner sc = new Scanner(System.in);

	void run() {

		int v = sc.nextInt();
		int e = sc.nextInt();

		E[] edges = new E[e * 2];
		for (int i = 0; i < e; i++) {
			E ed = new E();
			ed.from = sc.nextInt();
			ed.to = sc.nextInt();
			ed.cap = sc.nextInt();

			E ed2 = new E();
			ed2.from = ed.to;
			ed2.to = ed.from;
			ed2.cap = 0;

			edges[i] = ed;
			edges[i + e] = ed2;

		}

		long ans = 0;
		int[] depth = new int[v];
		boolean[] visited = new boolean[v];
		for (;;) {
			Arrays.fill(depth, v + 100);
			Arrays.fill(visited, false);
			if (!bfs(edges, depth, visited, 0, v - 1)) {
				break;
			}

			for (;;) {
				Arrays.fill(visited, false);
				visited[0] = true;
				int flow = dfs(10000,edges, depth,visited, 0, v - 1,0);
				if(flow == 0){
					break;
				}
				ans += flow;
			}
		}

		System.out.println(ans);
	}

	int dfs(int flow ,E[] edges, int[]depth,boolean[] visited, int now,int to,int d){
		if(now == to){
			return flow;
		}
		long ans = 0;
		for(int ii = 0 ; ii < edges.length; ii++){
			E e =edges[ii];
			if(e.from == now && !visited[e.to] && depth[e.to] > depth[e.from] && e.cap > 0){
				visited[e.to] = true;
				
				int res = dfs(Math.min(flow,e.cap),edges,depth,visited,e.to,to,d+1);
				if(res > 0 ){
					e.cap -= res;
					edges[(ii+edges.length/2)%edges.length].cap += res;
					return res;
				}
				visited[e.to] = false;
			}
		}
		return 0;
	}
	
	
	boolean bfs(E[] edges, int[] depth, boolean[] visited, int from, int to) {

		PriorityQueue<EE> q = new PriorityQueue<>((ee1, ee2) -> Integer.compare(ee1.depth, ee2.depth));
		EE init = new EE();
		init.depth = 0;
		init.pos = from;
		q.add(init);

		for (; !q.isEmpty();) {

			EE x = q.poll();
			if (x.pos == to) {
				depth[x.pos] = x.depth;
				return true;
			}

			if (depth[x.pos] <= x.depth) {
				continue;
			}
			depth[x.pos] = x.depth;
			visited[x.pos] = true;

			for (E e : edges) {
				if (e.from == x.pos && !visited[e.to] && e.cap > 0) {
					EE ex = new EE();
					ex.depth = x.depth + 1;
					ex.pos = e.to;
					q.add(ex);
				}
			}
		}
		return false;
	}

	class EE {
		int pos;
		int depth;
	}

	class E {
		int from;
		int to;
		int cap;
	}

}