From acfff7885bb465cee82cf1eb4a1213f900d2f0db Mon Sep 17 00:00:00 2001 From: Joshua Andersson Date: Tue, 23 Jun 2026 12:06:25 +0200 Subject: [PATCH] Simplify MCMF --- content/graph/MinCostMaxFlow.h | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/content/graph/MinCostMaxFlow.h b/content/graph/MinCostMaxFlow.h index 23819ceed..700e2fee2 100644 --- a/content/graph/MinCostMaxFlow.h +++ b/content/graph/MinCostMaxFlow.h @@ -10,8 +10,6 @@ */ #pragma once -// #include /// include-line, keep-include - const ll INF = numeric_limits::max() / 4; struct MCMF { @@ -21,11 +19,10 @@ struct MCMF { }; int N; vector> ed; - vi seen; vector dist, pi; vector par; - MCMF(int N) : N(N), ed(N), seen(N), dist(N), pi(N), par(N) {} + MCMF(int N) : N(N), ed(N), dist(N), pi(N), par(N) {} void addEdge(int from, int to, ll cap, ll cost) { if (from == to) return; @@ -34,26 +31,21 @@ struct MCMF { } void path(int s) { - fill(all(seen), 0); fill(all(dist), INF); - dist[s] = 0; ll di; + dist[s] = 0; - __gnu_pbds::priority_queue> q; - vector its(N); + priority_queue> q; q.push({ 0, s }); while (!q.empty()) { - s = q.top().second; q.pop(); - seen[s] = 1; di = dist[s] + pi[s]; - for (edge& e : ed[s]) if (!seen[e.to]) { - ll val = di - pi[e.to] + e.cost; + auto [d,u] = q.top(); q.pop(); + if (-d > dist[u]) continue; + for (edge& e : ed[u]) { + ll val = pi[u] - d - 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 }); + q.push({ -dist[e.to], e.to }); } } } @@ -62,7 +54,7 @@ struct MCMF { pair maxflow(int s, int t) { ll totflow = 0, totcost = 0; - while (path(s), seen[t]) { + while (path(s), dist[t]!=INF) { ll fl = INF; for (edge* x = par[t]; x; x = par[x->from]) fl = min(fl, x->cap - x->flow);