1
0
Fork 0

Update graph/mcmf.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-10-21 19:50:50 +08:00
parent 92df85050d
commit 1496b2c80c
1 changed files with 3 additions and 6 deletions

View File

@ -7,19 +7,15 @@ struct mcmf {
int rev; int rev;
int mark; int mark;
}; };
vector<vector<edge>> edges; vector<vector<edge>> edges;
vector<ll> dis; vector<ll> dis;
vector<bool> vis; vector<bool> vis;
ll ret; ll ret;
mcmf(int n) : edges(n + 1), dis(n + 1), vis(n + 1) {} mcmf(int n) : edges(n + 1), dis(n + 1), vis(n + 1) {}
void add_edge(int from, int to, ll cap, ll cost, int mark = 0, int mark_rev = 0) { void add_edge(int from, int to, ll cap, ll cost, int mark = 0, int mark_rev = 0) {
edges[from].push_back({ to, cap, 0, cost, int(edges[to].size()), mark }); edges[from].push_back({ to, cap, 0, cost, int(edges[to].size()), mark });
edges[to].push_back({ from, 0, 0, -cost, int(edges[from].size() - 1), mark_rev }); edges[to].push_back({ from, 0, 0, -cost, int(edges[from].size() - 1), mark_rev });
} }
bool sp(int s, int t) { bool sp(int s, int t) {
dis.assign(edges.size(), INFLL); dis.assign(edges.size(), INFLL);
dis[s] = 0; dis[s] = 0;
@ -38,7 +34,6 @@ struct mcmf {
} }
return dis[t] != INFLL; return dis[t] != INFLL;
} }
ll dfs(int s, int t, ll cap) { ll dfs(int s, int t, ll cap) {
if (vis[s]) { if (vis[s]) {
return 0; return 0;
@ -64,7 +59,6 @@ struct mcmf {
} }
return res; return res;
} }
// returns: (flow, cost) // returns: (flow, cost)
pll run(int s, int t) { pll run(int s, int t) {
ll res = 0; ret = 0; ll res = 0; ret = 0;
@ -72,6 +66,9 @@ struct mcmf {
vis.assign(edges.size(), 0); vis.assign(edges.size(), 0);
ll curr = dfs(s, t, LLONG_MAX); ll curr = dfs(s, t, LLONG_MAX);
res += curr; res += curr;
// BUG: this is a temporary fix of the infinite-looping issue observed
// when dealing with networks with negative weights.
if (curr == 0) break;
} }
return { res, ret }; return { res, ret };
} }