转:
int n, m, s = 0, t = n + m + 1, k;
int bfs() {
memset(depth, 0, sizeof(depth)); queue<int> q; q.push(s); depth[s] = 1;
while (q.size()) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (depth[v] || e[i].flow <= 0) continue;
depth[v] = depth[u] + 1;
q.push(v);
if (v == t) return 1;
}
}
return 0;
}
int now[maxn], sum = 0;
int dinic(int x, int lim) {
if (x == t) return lim;
int sum = 0;
for (int i = now[x]; i && lim; now[x] = i, i = e[i].next) {
int v = e[i].to;
if (depth[v] != depth[x] + 1 || e[i].flow <= 0) continue;
int f = dinic(v, min(e[i].flow, lim));
sum += f, lim -= f, e[i].flow -= f, e[i ^ 1].flow += f;
}
if (!sum) depth[x] = 0;
return sum;
}
main() {
while (bfs()) {
memcpy(now, head, sizeof(head));
while (1) {
int cur = dinic(s, inf);
ans += cur;
if (!cur) break;
}
}
}
原文:https://www.cnblogs.com/hellohhy/p/13963282.html