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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int V, E;
        public Graph G;
        public void Run()
        {
            {
                var line = Input.ReadIntArray();
                V = line[0];
                E = line[1];
            }

            G = new Graph(V);
            for (int i = 0; i < E; i++)
            {
                var line = Input.ReadIntArray();
                int fr = line[0];
                int to = line[1];
                int cap = line[2];
                var e1 = new Edge(fr, to, cap);
                var e2 = new Edge(to, fr, 0);
                e1.Rev = e2;
                e2.Rev = e1;
                G[fr].Edges.Add(e1);
                G[to].Edges.Add(e2);
            }

            var ff = new FordFulkerson(G);
            int ret = ff.MaxFlow(0, V - 1);
            Console.WriteLine(ret);
        }
    }

    // libs ----------
    // 最大流問題、最悪で計算量はO(F*E)
    class FordFulkerson
    {
        public bool[] Used;
        int N;
        public Graph G;
        private int INF = 1000000;

        public FordFulkerson(Graph G)
        {
            this.G = G;
            N = G.Nodes.Count;
        }

        public int MaxFlow(int s, int t)
        {
            int ret = 0;
            while (true)
            {
                Used = new bool[N];
                int flow = DFS(s, t, INF);
                if (flow == 0) return ret;
                ret += flow;
            }
        }

        public int DFS(int ss, int tt, int flow)
        {
            if (ss == tt) return flow;
            Used[ss] = true;
            foreach (var e in G[ss].Edges)
            {
                if (!Used[e.To] && e.Cap > 0)
                {
                    int f = DFS(e.To, tt, Math.Min(flow, e.Cap));
                    if (f > 0)
                    {
                        e.Cap -= f;
                        e.Rev.Cap += f;
                        return f;
                    }
                }
            }

            return 0;
        }
    }

    internal class Graph
    {
        public List<Node> Nodes;

        public Node this[int i]
        {
            set { Nodes[i] = value; }
            get { return Nodes[i]; }
        }

        public Graph(int N)
        {
            Nodes = new List<Node>();
            for (int i = 0; i < N; i++)
            {
                Nodes.Add(new Node(i));
            }
        }

    }

    internal class Node
    {
        public List<Edge> Edges;
        public int No;

        public Node(int no)
        {
            this.No = no;
            Edges = new List<Edge>();
        }
    }

    internal class Edge
    {
        public int From;
        public int To;
        public int Cap;
        public Edge Rev;

        public Edge(int fr, int to, int cap)
        {
            From = fr;
            To = to;
            Cap = cap;
        }

    }

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}