Skip to content

Commit

Permalink
Improve MCMF (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matistjati authored Jan 14, 2024
1 parent 782a5f4 commit 7868c30
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions content/graph/MinCostMaxFlow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
* Author: Stanford
* Date: Unknown
* Source: Stanford Notebook
* Description: Min-cost max-flow. cap[i][j] != cap[j][i] is allowed; double edges are not.
* Description: Min-cost max-flow.
* If costs can be negative, call setpi before maxflow, but note that negative cost cycles are not supported.
* To obtain the actual flow, look at positive values only.
* Status: Tested on kattis:mincostmaxflow, stress-tested against another implementation
* Time: Approximately O(E^2)
* Time: $O(F E \log(V))$ where F is max flow. $O(VE)$ for setpi.
*/
#pragma once

// #include <bits/extc++.h> /// include-line, keep-include

const ll INF = numeric_limits<ll>::max() / 4;
typedef vector<ll> VL;

struct MCMF {
struct edge {
int from, to, rev;
ll cap, cost, flow;
};
int N;
vector<vi> ed, red;
vector<VL> cap, flow, cost;
vector<vector<edge>> ed;
vi seen;
VL dist, pi;
vector<pii> par;
vector<ll> dist, pi;
vector<edge*> par;

MCMF(int N) :
N(N), ed(N), red(N), cap(N, VL(N)), flow(cap), cost(cap),
seen(N), dist(N), pi(N), par(N) {}
MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {}

void addEdge(int from, int to, ll cap, ll cost) {
this->cap[from][to] = cap;
this->cost[from][to] = cost;
ed[from].push_back(to);
red[to].push_back(from);
if (from == to) return;
ed[from].push_back(edge{ from,to,sz(ed[to]),cap,cost,0 });
ed[to].push_back(edge{ to,from,sz(ed[from])-1,0,-cost,0 });
}

void path(int s) {
Expand All @@ -41,25 +40,22 @@ struct MCMF {

__gnu_pbds::priority_queue<pair<ll, int>> q;
vector<decltype(q)::point_iterator> its(N);
q.push({0, s});

auto relax = [&](int i, ll cap, ll cost, int dir) {
ll val = di - pi[i] + cost;
if (cap && val < dist[i]) {
dist[i] = val;
par[i] = {s, dir};
if (its[i] == q.end()) its[i] = q.push({-dist[i], i});
else q.modify(its[i], {-dist[i], i});
}
};
q.push({ 0, s });

while (!q.empty()) {
s = q.top().second; q.pop();
seen[s] = 1; di = dist[s] + pi[s];
for (int i : ed[s]) if (!seen[i])
relax(i, cap[s][i] - flow[s][i], cost[s][i], 1);
for (int i : red[s]) if (!seen[i])
relax(i, flow[i][s], -cost[i][s], 0);
for (edge& e : ed[s]) if (!seen[e.to]) {
ll val = di - pi[e.to] + e.cost;
if (e.cap - e.flow > 0 && val < dist[e.to]) {
dist[e.to] = val;
par[e.to] = &e;
if (its[e.to] == q.end())
its[e.to] = q.push({ -dist[e.to], e.to });
else
q.modify(its[e.to], { -dist[e.to], e.to });
}
}
}
rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
}
Expand All @@ -68,15 +64,17 @@ struct MCMF {
ll totflow = 0, totcost = 0;
while (path(s), seen[t]) {
ll fl = INF;
for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
fl = min(fl, r ? cap[p][x] - flow[p][x] : flow[x][p]);
for (edge* x = par[t]; x; x = par[x->from])
fl = min(fl, x->cap - x->flow);

totflow += fl;
for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
if (r) flow[p][x] += fl;
else flow[x][p] -= fl;
for (edge* x = par[t]; x; x = par[x->from]) {
x->flow += fl;
ed[x->to][x->rev].flow -= fl;
}
}
rep(i,0,N) rep(j,0,N) totcost += cost[i][j] * flow[i][j];
return {totflow, totcost};
rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
return {totflow, totcost/2};
}

// If some costs can be negative, call this before maxflow:
Expand All @@ -85,9 +83,9 @@ struct MCMF {
int it = N, ch = 1; ll v;
while (ch-- && it--)
rep(i,0,N) if (pi[i] != INF)
for (int to : ed[i]) if (cap[i][to])
if ((v = pi[i] + cost[i][to]) < pi[to])
pi[to] = v, ch = 1;
for (edge& e : ed[i]) if (e.cap)
if ((v = pi[i] + e.cost) < pi[e.to])
pi[e.to] = v, ch = 1;
assert(it >= 0); // negative cost cycle
}
};

0 comments on commit 7868c30

Please sign in to comment.