Maximum flow · Hard
A replication topology has directed capacity limits. Flow may split across paths, and reverse residual routes may be needed to improve an earlier allocation.
Return maximum source-to-sink flow.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ n:integer, edges:[from:integer,to:integer,capacity:integer][], source:integer, sink:integer }
Input rules
- Parallel capacities add; source and sink differ.
What each field means
n— number of nodes with IDs0throughn - 1.- Each
[from, to, capacity]is a directed link with maximum flow capacity. sourceandsink— distinct start and destination nodes. Parallel link capacities add.
Output
Return value and rules
- Return the maximum total flow that can reach the sink from the source.
- Do not exceed any edge capacity. At every other node, incoming and outgoing flow must balance.
- Return a non-negative number, not routes or per-edge flows.
Constraints
- 2 <= n <= 60; 0 <= edges.length <= 500
- Capacities in [0,1000000]; endpoints valid; self-edges are allowed and contribute no useful flow.
Examples
Example 1
{
"edges": [
[
0,
1,
3
],
[
0,
2,
2
],
[
1,
2,
1
],
[
1,
3,
2
],
[
2,
3,
3
]
],
"n": 4,
"sink": 3,
"source": 0
}Returns
5Example 2
{
"edges": [],
"n": 2,
"sink": 1,
"source": 0
}Returns
0Required complexity
O(V × E²) time and O(V² + E) space