From be32379ba9f2de31d7c3943dcf06e362e576bfde Mon Sep 17 00:00:00 2001 From: Tai Tze Kin <90845551+teekaytai@users.noreply.github.com> Date: Fri, 15 Mar 2024 14:32:37 +0800 Subject: [PATCH] Update mcmf.cpp to simplify updates to total_cost --- ch9/mcmf.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ch9/mcmf.cpp b/ch9/mcmf.cpp index 9335e78..419d8c7 100644 --- a/ch9/mcmf.cpp +++ b/ch9/mcmf.cpp @@ -51,7 +51,6 @@ class min_cost_max_flow { auto &[v, cap, flow, cost] = EL[AL[u][i]]; if (!vis[v] && d[v] == d[u]+cost) { // in current layer graph if (ll pushed = DFS(v, t, min(f, cap-flow))) { - total_cost += pushed * cost; flow += pushed; auto &[rv, rcap, rflow, rcost] = EL[AL[u][i]^1]; // back edge rflow -= pushed; @@ -86,8 +85,10 @@ class min_cost_max_flow { ll mf = 0; // mf stands for max_flow while (SPFA(s, t)) { // an O(V^2*E) algorithm last.assign(V, 0); // important speedup - while (ll f = DFS(s, t)) // exhaust blocking flow + while (ll f = DFS(s, t)) { // exhaust blocking flow mf += f; + total_cost += f * d[t]; + } } return {mf, total_cost}; }