Maximum flow · Hard
A network planning tool estimates how much integral throughput can pass from one node to another. Directed links have independent capacities, and multiple links may connect the same endpoints. A locally attractive route can consume capacity that a better combination needs, so residual rerouting matters.
Return the maximum total integral flow from source to sink while respecting every link capacity and flow conservation at other nodes.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ n: integer, edges: [integer, integer, integer][], source: integer, sink: integer }
Fields
- Nodes are indexes 0..n-1. An edge [u,v,capacity] is directed; parallel links add capacity and opposite-direction links are independent.
- source and sink are distinct valid indexes. Capacities are non-negative integers; zero-capacity links and self-loops are permitted.
Output
Return value
- Return one non-negative integer flow value, not a route or residual graph.
- An unreachable sink returns zero. Self-loops cannot increase net source-to-sink flow.
Constraints
- 2 <= n <= 80; 0 <= edges.length <= 1500; 0 <= capacity <= 1000000.
- All endpoints are valid. Total capacity fits a safe JSON integer. Do not mutate input.
Examples
Example 1
{
"edges": [
[
0,
1,
3
],
[
0,
2,
2
],
[
1,
3,
2
],
[
2,
3,
4
],
[
1,
2,
1
]
],
"n": 4,
"sink": 3,
"source": 0
}Returns
5Example 2
{
"edges": [
[
0,
1,
2
],
[
0,
1,
3
],
[
1,
0,
9
]
],
"n": 2,
"sink": 1,
"source": 0
}Returns
5Required complexity
O(V * E^2) time for breadth-first augmenting paths and O(V^2 + E) space for a capacity matrix plus adjacency