MinCostFlow

It solves Minimum-cost flow problem.

Constructor

mcf_graph<Cap, Cost> graph(int n);

It creates a directed graph with $n$ vertices and $0$ edges. Cap and Cost are the type of the capacity and the cost, respectively.

Constraints

Complexity

add_edge

int graph.add_edge(int from, int to, Cap cap, Cost cost);

It adds an edge oriented from from to to with capacity cap and cost cost. It returns an integer $k$ such that this is the $k$-th edge that is added.

Constraints

Complexity

min_cost_max_flow

(1) pair<Cap, Cost> graph.flow(int s, int t);
(2) pair<Cap, Cost> graph.flow(int s, int t, Cap flow_limit);

Constraints

Complexity

min_cost_slope

vector<pair<Cap, Cost>> graph.slope(int s, int t);
vector<pair<Cap, Cost>> graph.slope(int s, int t, Cap flow_limit);

Let $g$ be a function such that $g(x)$ is the cost of the minimum cost $s-t$ flow when the amount of the flow is exactly $x$. $g$ is known to be piecewise linear. It returns $g$ as the list of the changepoints, that satisfies the followings.

Constraints

Let $x$ be the maximum cost among all edges.

Complexity

edges

struct edge<Cap, Cost> {
    int from, to;
    Cap cap, flow;
    Cost cost;
};

(1) mcf_graph<Cap, Cost>::edge graph.get_edge(int i);
(2) vector<mcf_graph<Cap, Cost>::edge> graph.edges();

Constraints

Complexity

Examples

AC code of https://atcoder.jp/contests/practice2/tasks/practice2_e

include atcoder/header import atcoder/mincostflow let n, k = nextInt() var g = initMCFGraph[int,int](n * 2 + 2) let (s, t) = (n * 2, n * 2 + 1) let BIG = 1000000000 g.add_edge(s, t, n * k, BIG) for i in 0..<n: g.add_edge(s, i, k, 0) g.add_edge(n + i, t, k, 0) for i in 0..<n: for j in 0..<n: let a = nextInt() g.add_edge(i, n + j, 1, BIG - a) var result = g.flow(s, t, n * k) echo n * k * BIG - result[1] var grid = newSeqWith(n, '.'.repeat(n)) let es = g.edges() for e in es: if e.src == s or e.dst == t or e.flow == 0: continue grid[e.src][e.dst - n] = 'X' for i in 0..<n: echo grid[i]