1113. Pipeline Capacity

Hard7.5 points estimatedGraphBreadth-First Search3 hints

A refinery network has n junctions numbered 0 to n - 1. Each entry [u, v, c] of edges describes a one-way pipe from junction u to junction v that can carry at most c barrels of oil per hour. Oil is pumped in at junction source and drawn off at junction sink; every other junction must pass on exactly as much oil as it receives, and a pipe may carry any whole amount from 0 up to its capacity.

Return the largest number of barrels per hour that can reach sink. Several pipes may join the same pair of junctions (they work independently), a pipe from a junction to itself is useless, and the answer is 0 when the sink cannot be reached.

Example 1

Input:
n = 4edges = [[0,1,5],[0,2,3],[1,3,4],[2,3,6]]source = 0sink = 3
Output:
7
Explanation:

The upper route 0-1-3 is limited to 4 barrels by pipe 1-3 and the lower route 0-2-3 to 3 barrels by pipe 0-2, so 4 + 3 = 7 can flow.

Example 2

Input:
n = 4edges = [[0,1,6],[0,2,6],[1,2,6],[1,3,6],[2,3,6]]source = 0sink = 3
Output:
12
Explanation:

Both pipes leaving the source hold 6 barrels and can be fully used by sending 6 along 0-1-3 and 6 along 0-2-3, giving 12.

Example 3

Input:
n = 3edges = [[1,0,5],[1,2,4]]source = 0sink = 2
Output:
0
Explanation:

Every pipe points away from the sink side or into the source, so nothing can leave junction 0 and the answer is 0.

Constraints

2 ≤ n ≤ 3000

0 ≤ edges.length ≤ 24000, each edge is [u, v, c] with 0 ≤ u, v < n and 1 ≤ c ≤ 1000

0 ≤ source, sink < n and source ≠ sink

The answer always fits in a 32-bit signed integer.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(V * E^2)
Space
O(V + E)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…