CP Python LibraryGitHub

Min Cost Flow

Solves minimum cost (b-)flow problem.

As of 2025/12/31, this implementation is the fastest in Python3 (PyPy).

__init__

Arguments

add_supply

Adds a supply of amount units to a node if amount > 0 or a demand of -amount units if amount < 0.

Arguments

add_edge

Appends an edge with capacity to the graph.

Arguments

solve

Finds the minimum cost of the b-flow.

Returns

Complexities

where mm represents the number of edges and supplies added, and UU represents the maximum capacity (upper - lower) of the edges.

edges

Returns the edges of the graph with flow that achieves the minimum cost.

Note: you must call solve before using this method.

Returns

Code Test