Minimum-Cost B-Flow

Solves a minimum-cost B-flow problem with lower and upper edge bounds, per-vertex supplies and demands, and edge costs.

General form

var flow =
  initBFlow[Capacity, Cost](
    vertexCount,
  )

flow.add_supply(
  vertex,
  supply,
)

flow.add_demand(
  vertex,
  demand,
)

flow.add_edge(
  source,
  target,
  lowerCapacity,
  upperCapacity,
  cost,
)

let (feasible, minimumCost) =
  flow.solve()

Parameters and types

Edges are numbered in insertion order. Parallel edges and negative edge costs are supported.

Preconditions

Adding an edge with lower > upper sets flow.infeasible to true.

For general feasibility, always use the first component returned by solve(), not flow.infeasible. Capacity shortage or unequal total supply and demand can make solve()[0] false while flow.infeasible remains false.

Return values and side effects

let (feasible, minimumCost) =
  flow.solve()

The authoritative general feasibility result is solve()[0].

Example

import atcoder/extra/graph/bflowvar flow =  initBFlow[int, int](2)flow.add_supply(  0,  2,)flow.add_demand(  1,  2,)flow.add_edge(  0,  1,  2,  5,  3,)let answer =  flow.solve()doAssert answer ==  (true, 6)doAssert flow.f ==  @[2]

This module combines lower and upper bounds, vertex balances, and costs in one B-flow formulation. Internally, it uses maximum flow and a cost-scaling minimum-cost-flow solver.

Complexity and implementation

The detailed bound for solve depends on the internal MFGraph and MCFCostScaling implementations.