From 6c0c82c0221b9fac5f3eefaa418e777c7891bf16 Mon Sep 17 00:00:00 2001 From: subcrip Date: Wed, 18 Sep 2024 14:17:16 +0800 Subject: [PATCH] Update graph/bounded_mcmf.cc Signed-off-by: subcrip --- graph/bounded_mcmf.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/graph/bounded_mcmf.cc b/graph/bounded_mcmf.cc index 6679553..b474fd2 100644 --- a/graph/bounded_mcmf.cc +++ b/graph/bounded_mcmf.cc @@ -1,11 +1,11 @@ struct bounded_mcmf { int n, m, S, T; mcmf net; - ll sum; + ll sum, pre; vector fl; vector init; vector costs; - bounded_mcmf(int n, int m) : sum(0), n(n), m(m), S(0), T(n + 1), net(n + 1), fl(m), init(n + 1), costs(m) {} + bounded_mcmf(int n, int m) : sum(0), pre(0), n(n), m(m), S(0), T(n + 1), net(n + 1), fl(m), init(n + 1), costs(m) {} // handle negative loop case void add_edge(int from, int to, ll low, ll high, ll cost, int edge_id = -1) { if (cost < 0) { @@ -26,6 +26,7 @@ struct bounded_mcmf { void __add_edge(int from, int to, ll low, ll high, ll cost, int edge_id = -1) { net.add_edge(from, to, high - low, cost, edge_id, -1); init[to] += low, init[from] -= low; + pre += low * cost; } void prep(int s, int t) { for (int i = 1; i <= n; ++i) { @@ -56,6 +57,7 @@ struct bounded_mcmf { } } } + res_cost += pre; return {{res_flow, res_cost, fl}}; } } @@ -63,6 +65,7 @@ struct bounded_mcmf { optional>> run_mcf(int s, int t) { prep(s, t); auto [res_flow, res_cost] = net.run(S, T); + res_cost += pre; if (sum != res_flow) { return nullopt; } else {