【CodeVS 1993】草地排水 isap模板题

开始网络流的学习,更新一下isap的模板

#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
using namespace std;
const int N = 403;
int getint() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
int cap[N], d[N], cur[N], point[N], nxt[N], gap[N], p[N], to[N], from[N], cnt = 1;
void ins(int x, int y, int z) {
nxt[++cnt] = point[x]; to[cnt] = y; cap[cnt] = z; from[cnt] = x; point[x] = cnt;
}
int isap(int s, int t, int n) {
int flow = 0, i, u, f = 0x7fffffff;
memset(gap, 0, sizeof(gap)); memset(d, 0, sizeof(d));
for(int i = 0; i <= n; ++i)
cur[i] = point[i];
u = s; gap[0] = n;
while (d[s] < n) {
for(i = cur[u]; i; i = nxt[i]) if (cap[i] && d[u] == d[to[i]] + 1) break;
if (i) {
cur[u] = i; p[to[i]] = i; u = to[i];
if (u == t) {
for(f = 0x7fffffff; u != s; u = from[p[u]]) f = min(f, cap[p[u]]);
for(u = t; u != s; u = from[p[u]]) cap[p[u]] -= f, cap[p[u] ^ 1] += f;
flow += f;
}
} else {
if (!(--gap[d[u]])) break;
d[u] = n;
cur[u] = point[u];
for(i = cur[u]; i; i = nxt[i])
if (cap[i] && d[u] > d[to[i]] + 1)
d[u] = d[to[i]] + 1;
++gap[d[u]]; if (u != s) u = from[p[u]];
}
}
return flow;
}
int main() {
memset(point, 0, sizeof(point));
int u, v, e, n, m;
read(m); read(n);
for(int i = 1; i <= m; ++i) {
read(u); read(v); read(e);
ins(u, v, e);
ins(v, u, 0);
}
printf("%d\n", isap(1, n, n));
return 0;
}

233

上一篇:Android Studio科普篇——2.误区


下一篇:PTA博客制作的模版