← All problemsFree · Python · 60 min

Algorithm design / guarantees under an adversary

The Night Bus Contracts

Schedule night buses that must meet route minimums, respect road limits and cost as little as possible. Price a small network, prove another impossible with a crowded set of stops, then write a solver whose answers carry their own proof.

0 / 3 graded parts passSaved in this browser · loading saved work…
Jump to your work ↓

A city runs night buses between its stops. Each route has a contract: at least a minimum number of buses must run it every night, and the road allows at most a maximum. Every bus costs money to run, except on routes an advertiser sponsors, where the city is paid. Buses end the night where they started, so at every stop as many buses arrive as leave. A schedule like that is a circulation, and the city wants the cheapest one.

[2, 5] · 3 [0, 4] · 2 [1, 4] · 1 [0, 3] · 4 [0, 3] · -8 [0, 2] · 1 [1, 2] · 2 Depot Market Station Harbour

Each route is labelled [minimum, maximum] · cost per bus. The advertiser pays 8 for every bus from Harbour to Depot.

Minimums make this harder than a flow problem. With no minimums the empty schedule is always possible, but with them there may be no schedule at all, and when there is one it is not obvious where to start. Two kinds of proof settle each case:

  • Prices. Give every stop a price p. A route u→v with cost c has reduced cost c+pu−pv. If every route with a positive reduced cost runs at its minimum and every route with a negative reduced cost runs at its maximum, no schedule is cheaper.
  • A crowded set of stops. If the minimums force more buses into a set of stops than its outgoing routes can carry, or more out than its incoming routes can bring, there is no schedule.

In code, the stops are numbered 0 to n−1 (Depot 0, Market 1, Station 2, Harbour 3 above), and a route is (u, v, low, cap, cost).

Background: Minimum-Cost Flow.

easy · Part A

Price the four stops

What does the cheapest schedule for the four stops in the figure cost? Count the advertiser's payments as negative costs.

Explore in the sandbox

Experiment here, then copy a rule into the editor and test its guarantees.