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)

#region ZIPPER
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using sc = Scanner;
using ci = CharInterpreter;
//using System.Numerics;
//using Geometry;
//using gl = Geometry.GeometryLibrary;
using grl = GraphLibrary;
class Program
{
    static void Main(string[] args)
    {
        Solver solver = new Solver();
        solver.Solve();
#if DEBUG
        System.Console.WriteLine("?¶?????????????????????????????????????????????????????????????");
        System.Console.ReadKey();
#endif

    }
}
/// <summary>
/// ?¨??????\????????????????????´???????????????????????????????????????
/// </summary>

#endregion ZIPPER

public class Solver
{
    #region IGNORE_ME
    public Solver()
    {
        //???????????¨?????????????????????????????????????????????
    }
    #endregion IGNORE_ME

    public int MOD = 1000000007;//10^9 + 7

    public void Solve()
    {
        int v = sc.NextInt();
        int e = sc.NextInt();
        List<grl.Edge>[] G = new List<grl.Edge>[v];
        for (int i = 0; i < v; i++)
        {
            G[i] = new List<grl.Edge>();
        }
        for (int i = 0; i < e; i++)
        {
            int[] p = sc.NextIntArray();
            grl.AddEdge(ref G, p[0], p[1], p[2]);
        }
        Console.WriteLine(grl.MaxFlow(G,v,0,v-1));
#if DEBUG
        Console.WriteLine("t");//local check
#endif
    }
}


public static class GraphLibrary
{

    public struct Edge
    {
        /// <summary>
        /// ?????§????????´???????????? rev
        /// </summary>
        public int Rev;
        public int To;
        /// <summary>
        /// ?????§????????´????????????
        /// </summary>
        public int Cap;

        public Edge(int to, int cap, int rev)
        {
            this.To = to;
            this.Cap = cap;
            this.Rev = rev;
        }
    }

    public class Graph
    {
        public List<Edge>[] G;
        /// <summary>
        /// ?????????????????????????????? 0 ~ v-1;
        /// </summary>
        /// <param name="v">????????°</param>
        public Graph(int v)
        {
            G = new List<Edge>[v];
            for (int i = 0; i < G.Length; i++)
            {
                G[i] = new List<Edge>();
            }
        }

        public void AddEdge(Edge edge)
        {
            G[edge.Rev].Add(edge);
        }
    }

    #region UTILITY
    static bool IsInfinity(int a)
    {
        return a == int.MaxValue;
    }  
    #endregion UTILITY

    /// <summary>
    /// ???????????????????±???????
    /// </summary>
    /// <param name="v">????????°</param>
    /// <param name="s">?§???? 0 ~ v-1</param>
    /// <param name="ed">???????????±</param>
    /// <returns>d[i]: s??????i?????????????????????</returns>
    public static int[] BellmanFord(int v, int s, Edge[] ed)
    {
        int[] d = new int[v];
        for (int i = 0; i < v; i++) d[i] = int.MaxValue;
        d[s] = 0;
        while (true)
        {
            bool update = false;

            for (int i = 0; i < ed.Length; i++)
            {
                Edge e = ed[i];
                if (!IsInfinity(d[e.Rev]) && !IsInfinity(e.Cap) && d[e.To] > d[e.Rev] + e.Cap)
                {
                    d[e.To] = d[e.Rev] + e.Cap;
                    update = true;
                }
            }
            if (!update) break;
        }
        return d;
    }
    /// <summary>
    /// ????????????????????¨??????????????????
    /// </summary>
    /// <param name="v">???????????°</param>
    /// <param name="ed">????????????</param>
    /// <returns>????????????????????¨??????????????????</returns>
    public static bool DoesHaveNegativeLoop(int v, Edge[] ed)
    {
        int[] d = new int[v];

            for (int i = 0; i < v; i++)
            {
                for (int j = 0; j < ed.Length; j++)
                {
                    Edge e = ed[j];
                    if (d[e.To] > d[e.Rev] + e.Cap)
                    {
                        d[e.To] = d[e.Rev] + e.Cap;
                        if (i == v - 1) return true;//??????????????´??°????????£??????????????????????????????
                    }
                }
            }
            return false;
    }
    /// <summary>
    /// ???????????????????????´??????????????§??????????????????
    /// </summary>
    /// <param name="v"></param>
    /// <param name="s"></param>
    /// <param name="ed"></param>
    /// <returns></returns>
    public static bool DoesHaveNegativeLoop(int v, int s, Edge[] ed)
    {
        Edge[] connectedNodesChecker = new Edge[ed.Length];
        for (int i = 0; i < ed.Length; i++)
        {
            connectedNodesChecker[i] = new Edge(ed[i].Rev, ed[i].To, 1);
        }
        int[] d = BellmanFord(v, s, connectedNodesChecker);
        HashSet<int> canReachNodesFromS = new HashSet<int>();
        for (int i = 0; i < v; i++)
            if (!IsInfinity(d[i]))canReachNodesFromS.Add(i);
        for (int i = 0; i < ed.Length; i++)
            if (canReachNodesFromS.Contains(ed[i].Rev)) connectedNodesChecker[i].Cap = ed[i].Cap;
        return DoesHaveNegativeLoop(v,connectedNodesChecker);

    }
    /// <summary>
    /// ???????????????????±???????
    /// </summary>
    /// <param name="graph">????¬?????????????</param>
    /// <returns>d[i][j]]: i??????j?????????????????????</returns>
    public static int[][] WarshallFloyd(int[][] graph)
    {
        int[][] d = new int[graph.Length][];
        for (int i = 0; i < d.Length; i++) d[i] = new int[graph[0].Length];
        for (int k = 0; k < graph.Length; k++)
        {
            for (int i = 0; i < graph.Length; i++)
            {
                for (int j = 0; j < graph.Length; j++)
                {
                    if (!IsInfinity(d[i][k]) && !IsInfinity(d[k][j]))
                    {
                        d[i][j] = Math.Min(d[i][j], d[i][k] + d[k][j]);
                    }
                }
            }
        }
        return d;
    }
    /// <summary>
    /// ??£????????°?????????????°???¨?????¨????±???????
    /// </summary>
    /// <param name="v">???????????°</param>
    /// <param name="ed">???????????? to, from ????°??????????????????????</param>
    /// <returns>????°???¨?????¨?????????</returns>
    public static int Kruskal(int v, Edge[] ed)
    {
        Array.Sort(ed, (a, b) => { return a.Cap - b.Cap; });
        UnionFindTree uf = new UnionFindTree(v);
        int res = 0;
        for (int i = 0; i < ed.Length; i++)
        {
            Edge e = ed[i];
            if (!uf.Same(e.To, e.Rev))
            {
                uf.Unite(e.To, e.Rev);
                res += e.Cap;
            }
        }
        return res;
    }

    public static void AddEdge(ref List<Edge>[] G, int from, int to, int cap)
    {
        G[from].Add(new Edge(to, cap, G[to].Count));
        G[to].Add(new Edge(from, 0, G[from].Count - 1));
    }

    public static int MaxFlow(List<Edge>[] G, int v , int s, int t)
    {
        MaxFlowSolver solver = new MaxFlowSolver(G, v);
        return solver.MaxFlow(s, t);
    }

    private class MaxFlowSolver
    {
        private List<Edge>[] G;
        private bool[] Used;

        public MaxFlowSolver(List<Edge>[] G, int v)
        {
            this.G = G;
            this.Used = new bool[G.Length];
        }

        private int Dfs(int v, int t, int f)
        {

            if (v == t) return f;
            Used[v] = true;
            for (int i = 0; i < G[v].Count; i++)
            {
                Edge e = G[v][i];
                if (!Used[e.To] && e.Cap > 0)
                {
                    int d = Dfs(e.To, t, Math.Min(f, e.Cap));
                    if (d > 0)
                    {
                        G[v][i] = new Edge(e.To, e.Cap - d, e.Rev);
                        G[e.To][e.Rev] = new Edge(G[e.To][e.Rev].To, G[e.To][e.Rev].Cap + d, G[e.To][e.Rev].Rev);;
                        return d;
                    }
                }
            }
            return 0;
        }

        public int MaxFlow(int s, int t)
        {
            int flow = 0;
            for (;;)
            {
                for (int i = 0; i < Used.Length; i++)
                {
                    Used[i] = false;
                }
                int f = Dfs(s, t, int.MaxValue);
#if DEBUG
                Console.WriteLine("f = "+ f);
#endif 
                if (f == 0) return flow;
                flow += f;
            }
        }
    }


    #region SUPPORT_INNER_CLASS
    /// <summary>
    /// ??????????????????????????????????????????????????????????????\???
    /// ?????????????????????
    /// </summary>
    private class PriorityQueueForGL
    {
        int[] array;
        int count = 0;
        Comparison<int> comparison;

        // <param name="comparison">int????????????????????????????????????????????????, (a,b)?????¨????????????int??? a_int - b_int (a > b) ??????????????????????????????</param>
        public void SetOptionData(Comparison<int> comparison, int MaxCount)
        {
            this.comparison = comparison;
            array = new int[MaxCount];
            count = 0;
        }

        /// <summary>
        /// ???????????????????????????????????????????????°??????????´???????????????????
        /// </summary>
        /// <param name="array">????±???????????????????</param>
        private void PushHeap(int elem)
        {
            int n = count;
            count++;
            array[n] = elem;
            while (n != 0)
            {
                int i = (n - 1) / 2;
                // ?????¨????????\?????????
                if (comparison(array[n], array[i]) < 0)
                {
                    int tmp = array[n]; array[n] = array[i]; array[i] = tmp;
                }
                n = i;
            }
        }

        /// <summary>
        /// ????????????????????????????????????????????????
        /// </summary>
        /// <param name="array">????±???????????????????</param>
        private int PopHeap()
        {
            if (count == 0)
                throw new IndexOutOfRangeException("????´????0????????\??????????´?????????????????????????????????????????????????");
            int n = count - 1;
            int returnItem = (int)array[0];
            array[0] = array[n];
            array[n] = 0;
            count--;
            for (int i = 0, j; (j = 2 * i + 1) < n; )
            {
                // ????????§???????????????????????¶
                if ((j != n - 1) && (comparison((int)array[j], (int)array[j + 1]) > 0))
                    j++;
                // ?????¨????????\?????????
                if (comparison((int)array[i], (int)array[j]) > 0)
                {
                    int tmp = (int)array[j]; array[j] = array[i]; array[i] = tmp;
                }
                i = j;
            }
            return returnItem;
        }

        /// <summary>
        /// ????´???????????????\???
        /// </summary>
        /// <param name="elem">?????\?????????????´?</param>
        public void Push(int elem)
        {
            PushHeap(elem);
        }


        /// <summary>
        /// ????´??????????????????????????????????????????¬????????§????????????????¶???????
        /// </summary>
        /// <returns></returns>
        public int Pop()
        {
            return PopHeap();
        }

        /// <summary>
        /// ????´??????¨????¶??????????
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < array.Length; i++) array[i] = 0;
        }
    }
     
    /// <summary>
    /// UF??¨????????¬??????
    /// </summary>
    class UnionFindTree
    {
        int[] Par;
        int[] Rank;
        /// <summary>
        /// n???????´???°???????????????UF??¨
        /// </summary>
        /// <param name="n"></param>
        public UnionFindTree(int n)
        {
            this.Par = new int[n];
            this.Rank = new int[n];
            for (int i = 0; i < n; i++)
            {
                Par[i] = i;
            }
        }
        /// <summary>
        /// ??¨??????????±???????
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private int Find(int x)
        {
            if (Par[x] == x)
                return x;
            else
            {
                Par[x] = Find(Par[x]);
                return Par[x];
            }
        }
        /// <summary>
        /// x??¨y????±??????????????????????
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void Unite(int x, int y)
        {
            x = Find(x);
            y = Find(y);
            if (x == y) return;
            if (Rank[x] < Rank[y])
            {
                Par[x] = y;
            }
            else
            {
                Par[y] = x;
                if (Rank[x] == Rank[y]) Rank[x]++;
            }
        }
        /// <summary>
        /// x??¨y???????????????????±????????????????
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Same(int x, int y)
        {
            return Find(x) == Find(y);
        }
    }

    #endregion SUPPORT_INNER_CLASS
}

#region STANDARD_READER
public static class Scanner
{
    public static string NextString()
    {
        string tmp = "";
        while (true)
        {
            int readData;
            string data;
            readData = Console.Read();
            if (readData == -1) //EOF
            {
                break;
            }
            data = char.ConvertFromUtf32(readData);
            if (data == " " || data == "\n")
            {
                break;
            }
            tmp += data;
        }
        return tmp;
    }
    public static int NextInt()
    {
        string tmp = "";
        while (true)
        {
            int readData;
            string data;
            readData = Console.Read();
            if (readData == -1) //EOF
            {
                break;
            }
            data = char.ConvertFromUtf32(readData);
            if (data == " " || data == "\n")
            {
                break;
            }
            tmp += data;
        }
        return int.Parse(tmp);
    }
    public static long NextLong()
    {
        string tmp = "";
        while (true)
        {
            int readData;
            string data;
            readData = Console.Read();
            if (readData == -1) //EOF
            {
                break;
            }
            data = char.ConvertFromUtf32(readData);
            if (data == " " || data == "\n")
            {
                break;
            }
            tmp += data;
        }
        return long.Parse(tmp);
    }
    public static double NextDouble()
    {
        string tmp = "";
        while (true)
        {
            int readData;
            string data;
            readData = Console.Read();
            if (readData == -1) //EOF
            {
                break;
            }
            data = char.ConvertFromUtf32(readData);
            if (data == " " || data == "\n")
            {
                break;
            }
            tmp += data;
        }
        return double.Parse(tmp);
    }

    public static string[] NextStrArray()
    {
        return Console.ReadLine().Split(' ');
    }
    public static int[] NextIntArray()
    {

        string[] s = NextStrArray();
        int[] a = new int[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = int.Parse(s[i]);
        }
        return a;
    }
    public static long[] NextLongArray()
    {
        string[] s = NextStrArray();
        long[] a = new long[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = long.Parse(s[i]);
        }
        return a;
    }
    public static double[] NextDoubleArray()
    {
        string[] s = NextStrArray();
        double[] a = new double[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = double.Parse(s[i]);
        }
        return a;
    }
}
/// <summary>
/// ????¬??????°?????????????????????????????§????????????????????????????????§???
/// ????????????????????????????????¨????????????????????????
/// </summary>
public static class CharInterpreter
{
    /// <summary>
    /// ????????¨??????
    /// </summary>
    private static Dictionary<char, int> MapToInt = new Dictionary<char, int>(); 
    /// <summary>
    /// ???????????????????????????
    /// </summary>
    /// <param name="c">char??????</param>
    /// <param name="i">??????????????´??°???</param>
    public static void AddCorrespondence(char c,int i)
    {
        MapToInt.Add(c,i);
    }
    /// <summary>
    /// ?????????????????????????????????????????????
    /// ???????????????????????????????????§??¨???
    /// </summary>
    /// <returns>???????????????????????£?????´???????????°?????????</returns>
    public static int Inquiry(char c)
    {
        int ret = 0;
        MapToInt.TryGetValue(c, out ret);
        return ret;
    }
    /// <summary>
    /// ???????????????????????????????????§int[,]???????¬?????????¢???????????????
    /// ????????¢??????????????´?????????????????????????????????????????§??¨???
    /// </summary>
    /// <param name="field">???????????????????????????????????¨???????????§????????¨?????????????????????????????§??¨???</param>
    /// <returns> int[ field.length , field[0].length]???????????? </returns>
    public static int[,] GenerateSquareField(string[] field)
    {
        int[,] ret = new int[field.Length, field[0].Length];
        for (int i = 0; i < field.Length; i++)
        {
            for (int j = 0; j < field[0].Length; j++)
            {
                MapToInt.TryGetValue(field[i][j], out ret[i, j]);
            }
        }
        return ret;
    }
}
#endregion STANDARD_READER