ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Drainage Ditches HDU 1532 (网络流 最大流 EK模板)

2019-07-13 10:36:17  阅读:271  来源: 互联网

标签:HDU EK int Ditches Farmer water incf each include


Every time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie’s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 210 
#define MOD
using namespace std;
const int inf=1<<29;
int n, m, T;
int head[N], ver[410], edge[410], Next[410], v[N], incf[N], pre[N];
int s, t, tot, maxflow;

void add(int x, int y, int z) {
	ver[++tot]=y, edge[tot]=z, Next[tot]=head[x], head[x]=tot;
	ver[++tot]=x, edge[tot]=0, Next[tot]=head[y], head[y]=tot;
}

bool bfs() {
	mem(v, 0);
	queue<int> q;
	q.push(s); v[s]=1;
	incf[s]=inf;
	while(q.size()) {
		int x=q.front(); q.pop();
		for(int i=head[x]; i; i=Next[i])
			if(edge[i]) {
				int y=ver[i];
				if(v[y]) continue;
				incf[y]=min(incf[x], edge[i]);
				pre[y]=i;
				q.push(y), v[y]=1;
				if(y==t) return 1;
			}
	}
	return 0;
}

void update() {
	int x=t;
	while(x!=s) {
		int i=pre[x];
		edge[i]-=incf[t];
		edge[i^1]+=incf[t];
		x=ver[i^1]; 
	}
	maxflow+=incf[t];
}

int main() {
	while(scanf("%d%d",&m, &n)!=EOF) {	 
		maxflow=0;
		mem(head, 0);
		s=1, t=n;
		tot=1;
		for(int i=1; i<=m; i++) {
			int x, y, z;
			scanf("%d%d%d",&x, &y, &z);
			add(x, y, z);
		}
		while(bfs()) update();
		cout<<maxflow<<endl;
	}
} 

标签:HDU,EK,int,Ditches,Farmer,water,incf,each,include
来源: https://blog.csdn.net/weixin_43326028/article/details/95727404

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有