Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Graphs/Maximum Flow / Minimum Cut

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).

Learn firstMaximum Flow / Minimum CutBellman-FordDijkstra (Weighted Shortest Path)Bipartite Matching
0%
mastery
0/6 solved
Practise nextTask Assignment
IdeaPracticePitfalls

Recognise it

The signal

assign n workers to n jobs at minimum total cost

  1. 1Brute force

    Try all assignments (n!), or bitmask DP in O(n 2n) for n <= 20.

  2. 2The observation

    Max flow ignores costs, and greedily taking the cheapest edges is not optimal.

  3. 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

Next

Task Assignment

0/6 solved
  • 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

    Task AssignmentGrid Puzzle II

Resources

  • Graph Theory playlistWilliam Fiset · video
  • Graph TraversalUSACO Guide · course
  • Disjoint Set Unioncp-algorithms · article
  • Dijkstracp-algorithms · article
  • Topological sortingcp-algorithms · article
  • Shortest paths visualizerVisuAlgo · tool
  • Graph cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems