FLOW-P3

A full plan, fast

Practice 3 of 5Network flow7 tests

The problem

Now return the plan itself: a pair (value, flow), where flow[i] is the number of trains an hour on line i in a maximum flow. Any maximum flow is accepted: the tests check your flow line by line (both rules of P1) and its value.

lines = [(0, 1, 10), (0, 2, 10), (1, 2, 2),
         (1, 3, 4), (1, 4, 8), (2, 4, 9),
         (4, 3, 6), (3, 5, 10), (4, 5, 10)]
flow_plan(6, lines, 0, 5)
# (19, [10, 9, 0, 4, 6, 9, 5, 9, 10])

The flow on line i is what its reverse arc holds at the end: the trains sent along it minus the trains sent back. Capacities are whole numbers up to 10⁹ (1,000,000,000).

Limits

  • Up to 10,000 stations and 50,000 lines. The big tests have 4 to 5 seconds each.
  • Paths found by depth-first search can carry very little each, so plain Ford-Fulkerson is too slow. Edmonds-Karp passes every test but the last.
  • The last test has 160 layers of stations; it needs Dinic’s method (lesson 3).

Builds on FLOW-3 Edmonds-Karp and Dinic.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

7 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2The diamondNot run
3Nothing to sendNot run
4Two ways roundNot run
52,000 stations, large capacitiesNot run
610,000 stations, capacities up to 10⁹Not run
7160 layers of 30Not run