FLOW-P4

The weakest set of lines

Practice 4 of 5Network flow7 tests

The problem

A cut splits the stations into a side S that holds s and the rest, which holds t. Its capacity is the total capacity of the lines from S to the rest; lines the other way do not count. Return a side S whose cut has the smallest capacity: the cheapest set of lines to close so that no train can get from s to t.

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)]
min_cut(6, lines, 0, 5)   # [0, 2]: it cuts the lines
                          # 0 -> 1 and 2 -> 4, 10 + 9 = 19

When several sides give the least capacity, any of them is accepted: the tests check that your side holds s and not t, and add up its cut.

Limits

  • Up to 10,000 stations and 50,000 lines, capacities up to 10⁹. The big tests have 4 seconds each.

Builds on FLOW-4 Max-flow min-cut.

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
2Two cuts of the same sizeNot run
3Nothing gets throughNot run
4Lines back across the cutNot run
5The diamondNot run
62,000 stationsNot run
710,000 stationsNot run