A flow network is a directed graph G = (V, E)
with a source vertex s ϵ V
and a sink vertex t ϵ V
, and where every edge (u, v) ϵ E
on the graph (connecting nodes u ϵ V
and v ϵ V
) has 2 quantities associated with it:
c(u, v) >= 0
, the capacity of the edgea(u, v) >= 0
, the cost of sending one unit through the edge
We define a function 0 <= f(u, v) <= c(u, v)
to be the number of units being passed through a given edge (u, v)
. Thus, the cost for a given edge (u, v)
is a(u, v) * f(u, v)
. The minimum-cost flow problem is defined as minimizing the total cost over all edges for a given flow amount d
, given by the following quantity:
The following constraints apply to the problem:
- Capacity requirements: the flow through a given edge may not exceed the capacity of that edge (
f(u, v) <= c(u, v)
). - Skew symmetry: the flow though a given edge must be antisymmetric when the direction is reversed (
f(u, v) = -f(v, u)
). - Flow conservation: the net flow into any non-sink non-source node must be 0 (for each
u ∉ {s, t}
, summing over allw
,sum f(u, w) = 0
). - Required flow: the net flow out of the source and the net flow into the sink must both equal the required flow through the network (summing over all
u
,sum f(s, u) = sum f(u, t) = d
).
Given a flow network G
and a required flow d
, output the minimum cost for sending d
units through the network. You may assume that a solution exists. d
and all capacities and costs will be non-negative integers. For a network with N
vertices labeled with [0, N-1]
, the source vertex will be 0
and the sink vertex will be N-1
.
This is code-golf, so the shortest answer (in bytes) wins. Remember that this is a competition within languages as well as between languages, so don't be afraid to post a solution in a verbose language.
Built-ins are allowed, but you are encouraged to include solutions without builtins, either as an additional solution in the same answer, or as an independent answer.
Input may be in any reasonable manner that includes the capacities and costs of each edge and the demand.
Test Cases
Test cases are provided in the following format:
c=<2D matrix of capacities> a=<2D matrix of costs> d=<demand> -> <solution>
c=[[0, 3, 2, 3, 2], [3, 0, 5, 3, 3], [2, 5, 0, 4, 5], [3, 3, 4, 0, 4], [2, 3, 5, 4, 0]] a=[[0, 1, 1, 2, 1], [1, 0, 1, 2, 3], [1, 1, 0, 2, 2], [2, 2, 2, 0, 3], [1, 3, 2, 3, 0]] d=7 -> 20
c=[[0, 1, 1, 5, 4], [1, 0, 2, 4, 2], [1, 2, 0, 1, 1], [5, 4, 1, 0, 3], [4, 2, 1, 3, 0]] a=[[0, 1, 1, 2, 2], [1, 0, 2, 4, 1], [1, 2, 0, 1, 1], [2, 4, 1, 0, 3], [2, 1, 1, 3, 0]] d=7 -> 17
c=[[0, 1, 4, 5, 4, 2, 3], [1, 0, 5, 4, 3, 3, 5], [4, 5, 0, 1, 5, 5, 5], [5, 4, 1, 0, 3, 2, 5], [4, 3, 5, 3, 0, 4, 4], [2, 3, 5, 2, 4, 0, 2], [3, 5, 5, 5, 4, 2, 0]] a=[[0, 1, 4, 2, 4, 1, 1], [1, 0, 3, 2, 2, 1, 1], [4, 3, 0, 1, 4, 5, 2], [2, 2, 1, 0, 2, 2, 3], [4, 2, 4, 2, 0, 4, 1], [1, 1, 5, 2, 4, 0, 2], [1, 1, 2, 3, 1, 2, 0]] d=10 -> 31
c=[[0, 16, 14, 10, 14, 11, 10, 4, 3, 16], [16, 0, 18, 19, 1, 6, 10, 19, 5, 4], [14, 18, 0, 2, 15, 9, 3, 14, 20, 13], [10, 19, 2, 0, 2, 10, 12, 17, 19, 22], [14, 1, 15, 2, 0, 11, 23, 25, 10, 19], [11, 6, 9, 10, 11, 0, 14, 16, 25, 4], [10, 10, 3, 12, 23, 14, 0, 11, 7, 8], [4, 19, 14, 17, 25, 16, 11, 0, 14, 5], [3, 5, 20, 19, 10, 25, 7, 14, 0, 22], [16, 4, 13, 22, 19, 4, 8, 5, 22, 0]] a=[[0, 12, 4, 2, 9, 1, 1, 3, 1, 6], [12, 0, 12, 16, 1, 2, 9, 13, 2, 3], [4, 12, 0, 2, 2, 2, 2, 10, 1, 1], [2, 16, 2, 0, 2, 1, 8, 4, 4, 2], [9, 1, 2, 2, 0, 5, 6, 23, 5, 8], [1, 2, 2, 1, 5, 0, 13, 12, 12, 1], [1, 9, 2, 8, 6, 13, 0, 9, 4, 4], [3, 13, 10, 4, 23, 12, 9, 0, 13, 1], [1, 2, 1, 4, 5, 12, 4, 13, 0, 13], [6, 3, 1, 2, 8, 1, 4, 1, 13, 0]] d=50 -> 213
These test cases were computed with the NetworkX Python library.