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

Minimum Cost Flow

Find the minimum cost to send a certain amount of flow through a flow network.

The flow network is a directed graph where each edge $e$ has capacity $c(e)$ and cost $d(e)$. Each edge $e$ can send an amount of flow $f(e)$ where $f(e) \leq c(e)$. Find the minimum value of $\sum_{e} (f(e) \times d(e))$ to send an amount of flow $F$ from source $s$ to sink $t$.

Input

A flow network $G(V,E)$ is given in the following format.

$|V|\;|E|\;F$

$u_0\;v_0\;c_0\;d_0$

$u_1\;v_1\;c_1\;d_1$

:

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

$|V|$, $|E|$, $F$ are the number of vertices, the number of edges, and the amount of flow 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$, $d_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$ and $d_i$ are the capacity and the cost of $i$-th edge respectively.

Output

Print the minimum value in a line. If it is impossible to send the flow from the source $s$ to the sink $t$, print -1.

Constraints

2 ≤ $|V|$ ≤ 100

1 ≤ $|E|$ ≤ 1000

0 ≤ $F$ ≤ 1000

0 ≤ $c_i$, $d_i$ ≤ 1000

$u_i$ $\ne$ $v_i$

If there is an edge from vertex $x$ to vertex $y$, there is no edge from vertex $y$ to vertex $x$.

Sample Input

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

Sample Output

6

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MinimumCostFlow
{
    class Edge
    {
        internal int to, cap, cost, rev;

        public Edge(int to, int cap, int cost, int rev)
        {
            this.to = to;
            this.cap = cap;
            this.cost = cost;
            this.rev = rev;
        }
    }

    class MinimumCostFlow
    {
        readonly int n, INFTY = int.MaxValue / 2;

        Dictionary<int, List<Edge>> G;

        public MinimumCostFlow(int V)
        {
            n = V;

            G = new Dictionary<int, List<Edge>>();
            for (int i = 0; i < n; i++) G.Add(i, new List<Edge>());
        }

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

        public int Solve(int s, int t, int f)
        {
            int ret = 0;

            int[] h = new int[n];
            int[] prevv = new int[n];
            int[] preve = new int[n];

            while (f > 0)
            {
                PriorityQueue<IntPair> que = new PriorityQueue<IntPair>();
                int[] dist = Enumerable.Repeat(INFTY, n).ToArray();
                dist[s] = 0;
                que.Enqueue(new IntPair(0, s));

                while (que.Count > 0)
                {
                    IntPair p = que.Dequeue();
                    int v = p.second;

                    if (dist[v] < p.first) continue;

                    for (int i = 0; i < G[v].Count; i++)
                    {
                        Edge e = G[v][i];
                        int u = e.to;

                        if (e.cap > 0 && dist[u] > dist[v] + e.cost + h[v] - h[u])
                        {
                            dist[u] = dist[v] + e.cost + h[v] - h[u];

                            prevv[u] = v;
                            preve[u] = i;
                            que.Enqueue(new IntPair(dist[u], u));
                        }
                    }
                }

                if (dist[t] == INFTY) return -1;

                for (int v = 0; v < n; v++) h[v] += dist[v];

                int d = f;

                for (int v = t; v != s; v = prevv[v])
                {
                    d = Math.Min(d, G[prevv[v]][preve[v]].cap);
                }

                f -= d;
                ret += d * h[t];

                for (int v = t; v != s; v = prevv[v])
                {
                    Edge e = G[prevv[v]][preve[v]];
                    e.cap -= d;
                    G[v][e.rev].cap += d;
                }
            }
            return ret;
        }
    }

    class PriorityQueue<T> where T : IComparable
    {
        List<T> heap;
        int size;

        public PriorityQueue()
        {
            heap = new List<T>();
        }

        public void Enqueue(T x)
        {
            heap.Add(x);
            int i = size++;

            while (i > 0)
            {
                int p = (i - 1) / 2;

                if (Compare(heap[p], x) <= 0) break;

                heap[i] = heap[p];
                i = p;
            }
            heap[i] = x;
        }

        public T Dequeue()
        {
            T ret = heap[0];
            T x = heap[--size];

            int i = 0;
            while (i * 2 + 1 < size)
            {
                int a = i * 2 + 1;
                int b = i * 2 + 2;

                if (b < size && Compare(heap[b], heap[a]) < 0)
                    a = b;
                if (Compare(heap[a], x) >= 0)
                    break;

                heap[i] = heap[a];
                i = a;
            }
            heap[i] = x;
            heap.RemoveAt(size);
            return ret;
        }

        public int Count
        {
            get { return size; }
        }

        private int Compare(T x, T y)
        {
            return y.CompareTo(x);
        }
    }

    class IntPair : IComparable
    {
        internal int first, second;

        public IntPair(int first, int second)
        {
            this.first = first;
            this.second = second;
        }

        public int CompareTo(object obj)
        {
            IntPair temp = obj as IntPair;

            return first - temp.first;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int V = int.Parse(input[0]);
            int E = int.Parse(input[1]);
            int F = int.Parse(input[2]);

            MinimumCostFlow MCF = new MinimumCostFlow(V);

            for (int i = 0; i < E; i++)
            {
                input = Console.ReadLine().Split(' ');
                int u = int.Parse(input[0]);
                int v = int.Parse(input[1]);
                int c = int.Parse(input[2]);
                int d = int.Parse(input[3]);

                MCF.AddEdge(u, v, c, d);
            }

            Console.WriteLine(MCF.Solve(0, V - 1, F));
        }
    }
}