Technique · CP 2000
Minimum-Cost Flow (and the assignment problem)
Augment along cheapest paths: Bellman-Ford/SPFA once, then Dijkstra with Johnson potentials. This gives the minimum total cost for each flow value. Weighted bipartite assignment is the special case that the Hungarian algorithm solves in O(n3).
0/6 solved
Recognise it
The signal
assign n workers to n jobs at minimum total cost
1Brute force
Try all assignments (n!), or bitmask DP in O(n 2n) for n <= 20.
2The observation
Max flow ignores costs, and greedily taking the cheapest edges is not optimal.
3Why it works
With reduced costs c(u,v) + p(u) - p(v) >= 0 on all residual edges Dijkstra is valid. Augmenting along a shortest path keeps the flow cost-optimal for its value.
Reach for it when
- +k disjoint routes with minimum total length
- +move units to targets where each move costs distance (transport problem)
Not when
- ×no costs (network-flow / bipartite-matching)
- ×n <= 15-20 (dp-bitmask is shorter to write)
Practice
- Task AssignmentCSESHard
- Distinct Routes IICSESHard
- Parcel DeliveryCSESHard
- Grid Puzzle IICSESHard
- Build StringCodeforces2000
- Four MelodiesCodeforces2600
Pitfalls
Misconceptions
- !Dijkstra on raw costs while negative residual edges exist (needs potentials)
- !treating weighted assignment as plain matching
Go deeper1 combinations · resources
Combinations
Minimum-Cost Flow (and the assignment problem)+Bipartite Matching
assign n employees to n tasks with a cost matrix, minimising the total
- Minimum-Cost Flow (and the assignment problem):
- minimises total cost over perfect matchings (Hungarian for dense n x n)
- Bipartite Matching:
- the left/right assignment structure