码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-04-11 22:32:03      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

开始网络流的学习,更新一下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

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

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5380039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!