poj1273 Drainage Ditches(最大流入门 EK+Dinic模板)

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 56697 Accepted: 21784

Description

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
/*
第一道网络流题目。事实证明,北大暑期课没有白上,理解了思想去看代码会很容易理解
今天晚上敲了三四遍,最后自己敲出来。
加油!!!
Time:2014-9-10 23:15
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<climits>
using namespace std;
const int MAX=205;
int N,M;
int map[MAX][MAX];
int Edmonds_Karp(int s,int e){
	int flow[MAX][MAX],pre[MAX];
	int path[MAX];
	int u,v,maxflow=0;
	queue<int >q;
	memset(flow,0,sizeof(flow));
	while(1){
		q.push(s);
		memset(path,0,sizeof(path));
		path[s]=INT_MAX;
		while(!q.empty()){
			u=q.front();q.pop();
			for(v=1;v<=M;v++){
				if(!path[v]&&map[u][v]>flow[u][v]){
					q.push(v);
					path[v]=min(path[u],map[u][v]-flow[u][v]);
					pre[v]=u;
				}
			}
		}
		if(path[e]==0)break;
		for(u=e;u!=s;u=pre[u]){
			flow[pre[u]][u]+=path[e];
			flow[u][pre[u]]-=path[e];
		}
		maxflow+=path[e];
	}
	return maxflow;
}
void solve(){
	while(scanf("%d%d",&N,&M)!=EOF){
		int u,v,w;
		memset(map,0,sizeof(map));
		for(int i=0;i<N;i++){
			scanf("%d%d%d",&u,&v,&w);
			map[u][v]+=w;//注意重边 
		}
		int ans=Edmonds_Karp(1,M);
		printf("%d\n",ans);
	}
}
int main(){
	//freopen("poj1273.txt","r",stdin);
	solve();
return 0;
}
/*
最大流 dinic 模板  
加油!!!
Time:2014-9-14 15:08
*/
#include<cstdio>
#include<cstring>
#include<climits>
#include<algorithm>
#include<queue>
using namespace std;
#define INF INT_MAX
const int MAX=205;
int N,M;
int map[MAX][MAX];
int d[MAX];
bool BFS(){
	queue<int >q;
	memset(d,-1,sizeof(d));
		d[1]=0;//将起点为 0 
		q.push(1);//将起点入队
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int v=1;v<=N;v++){
			if(map[u][v]&&d[v]==-1){
				d[v]=d[u]+1;
				q.push(v);
			}
		}
	}
	return d[N]!=-1; 
}
int DFS(int u,int cur_flow){
	if(u==N) return cur_flow;
	int temp=cur_flow;
	for(int v=1;v<=N;v++){
		if(map[u][v]>0&&d[u]+1==d[v]){
			int flow=DFS(v,min(temp,map[u][v]));//可以通过的最大流,即所有通道里边的最小流 
			map[u][v]-=flow;
			map[v][u]+=flow;
			
			temp-=flow;
		}
	}
	return cur_flow-temp;
} 
int Dinic(){
	int max_flow=0;
	while(BFS()){
		int temp;
		while(temp=DFS(1,INF)){//起点为1 
			max_flow+=temp; 
		}
	}
	return max_flow;
}
void solve(){
	while(scanf("%d%d",&M,&N)!=EOF){
		memset(map,0,sizeof(map));
		int u,v,w;
		for(int i=0;i<M;i++){
			scanf("%d%d%d",&u,&v,&w);
			map[u][v]+=w;//去重 
		}
		printf("%d\n",Dinic()); 
	}
}
int main(){
	solve();
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值