struct MCMF {
int fst[N], nxt[M], vv[M], from[M], cost[M], cap[M], flow[M], e;
int d[N], a[N], p[N], in[N];
void init() {
memset(fst, -1, sizeof fst);
e = 0;
}
void add(int u, int v, int c, int d) {
from[e] = u, vv[e] = v, nxt[e] = fst[u], cost[e] = d, cap[e] = c, flow[e] = 0, fst[u] = e++;
from[e] = v, vv[e] = u, nxt[e] = fst[v], cost[e] = -d, cap[e] = 0, flow[e] = 0, fst[v] = e++;
}
int spfa(int s, int t, int &mf, int &mc) {
memset(d, 0x3f, sizeof d);
d[s] = p[s] = 0;
a[s] = inf;
queue<int> q; q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop();
in[u] = 0;
for(int i = fst[u]; ~i; i = nxt[i]) {
int v = vv[i], c = cost[i];
if(cap[i] > flow[i] && d[v] > d[u] + c) {
d[v] = d[u] + c;
a[v] = min(a[u], cap[i] - flow[i]);
p[v] = i;
if(!in[v]) in[v] = 1, q.push(v);
}
}
}
if(d[t] == inf) return 0;
mf += a[t], mc += a[t] * d[t];
int u = t;
while(u != s) {
flow[p[u]] += a[t];
flow[p[u]^1] -= a[t];
u = from[p[u]];
}
return 1;
}
int gao(int s, int t) {
int ret = 0, mf = 0, mc = 0;
while(spfa(s, t, mf, mc)) ret = min(ret, mc);
return mc;
}
}go;
(模板) MCMF SPFA
最新推荐文章于 2023-03-08 00:16:04 发布