From 9dd10a201d7a1d1fba68f2468dbc6183a8d4945c Mon Sep 17 00:00:00 2001 From: subcrip Date: Mon, 20 May 2024 14:40:50 +0800 Subject: [PATCH] Update graph/bounded_flow.cc Signed-off-by: subcrip --- graph/bounded_flow.cc | 50 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/graph/bounded_flow.cc b/graph/bounded_flow.cc index aa1de5d..e936280 100644 --- a/graph/bounded_flow.cc +++ b/graph/bounded_flow.cc @@ -12,7 +12,7 @@ struct bounded_flow { net.add_edge(from, to, high - low, edge_id, -1); init[to] += low, init[from] -= low; } - optional>> run(int s, int t) { + void prep(int s, int t) { for (int i = 1; i <= n; ++i) { if (init[i] > 0) { net.add_edge(S, i, init[i], -1, -1); @@ -21,14 +21,58 @@ struct bounded_flow { net.add_edge(i, T, -init[i], -1, -1); } } - net.add_edge(t, s, INFLL, -1, -1); + net.add_edge(t, s, INFLL, INF, -1); + } + optional>> run_max_flow(int s, int t) { + prep(s, t); if (sum != net.run(S, T)) { return nullopt; } else { auto res_flow = net.run(s, t); for (int from = 1; from <= n; ++from) { for (auto&& [to, cap, flow, rev, mark] : net.edges[from]) { - if (mark != -1) { + if (mark != -1 and mark != INF) { + fl[mark] += flow; + } + } + } + return {{res_flow, fl}}; + } + } + optional>> run_min_flow(int s, int t) { + prep(s, t); + if (sum != net.run(S, T)) { + return nullopt; + } else { + int curr; + for (int i = 0; i < m; ++i) { + if (net.edges[t][i].mark == INF) { + net.edges[t][i].cap = 0; + net.edges[net.edges[t][i].to][net.edges[t][i].rev].cap = 0; + curr = net.edges[t][i].flow; // WARN: real flow + break; + } + } + curr -= net.run(t, s); + for (int from = 1; from <= n; ++from) { + for (auto&& [to, cap, flow, rev, mark] : net.edges[from]) { + if (mark != -1 and mark != INF) { + fl[mark] += flow; + } + } + } + return {{curr, fl}}; + } + } + optional>> run_flow(int s, int t) { // BUG: unchecked code + prep(s, t); + auto res_flow = net.run(S, T); + if (sum != res_flow) { + return nullopt; + } else { + for (int from = 1; from <= n; ++from) { + for (auto&& [to, cap, flow, rev, mark] : net.edges[from]) { + if (mark != -1 and mark != INF) { fl[mark] += flow; } }