1
0
Fork 0

Update graph/bounded_flow.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-05-20 14:40:50 +08:00
parent 5b56282a57
commit 9dd10a201d
1 changed files with 47 additions and 3 deletions

View File

@ -12,7 +12,7 @@ struct bounded_flow {
net.add_edge(from, to, high - low, edge_id, -1); net.add_edge(from, to, high - low, edge_id, -1);
init[to] += low, init[from] -= low; init[to] += low, init[from] -= low;
} }
optional<pair<ll, vector<ll>>> run(int s, int t) { void prep(int s, int t) {
for (int i = 1; i <= n; ++i) { for (int i = 1; i <= n; ++i) {
if (init[i] > 0) { if (init[i] > 0) {
net.add_edge(S, i, init[i], -1, -1); 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(i, T, -init[i], -1, -1);
} }
} }
net.add_edge(t, s, INFLL, -1, -1); net.add_edge(t, s, INFLL, INF, -1);
}
optional<pair<ll, vector<ll>>> run_max_flow(int s, int t) {
prep(s, t);
if (sum != net.run(S, T)) { if (sum != net.run(S, T)) {
return nullopt; return nullopt;
} else { } else {
auto res_flow = net.run(s, t); auto res_flow = net.run(s, t);
for (int from = 1; from <= n; ++from) { for (int from = 1; from <= n; ++from) {
for (auto&& [to, cap, flow, rev, mark] : net.edges[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<pair<ll, vector<ll>>> 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<pair<ll, vector<ll>>> 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; fl[mark] += flow;
} }
} }