POJ 1273 Drainage Ditches

原创 2015年11月19日 09:33:02

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 64253   Accepted: 24793

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
#define maxn 250
#define INF 0x3f3f3f3f  
int flow[maxn][maxn],a[maxn],p[maxn];
// flow 是流量,cap是容量,a[i]表示源点s到节点i的路径上的最小残留量
int cap[maxn][maxn];

int min(int a,int b){
	return a>b?b:a;
}

int m;

void EK(int st,int end){
	int u,v,i;
	int sum = 0;
	queue<int > q;
	memset(flow,0,sizeof(flow));
	memset(p,0,sizeof(p));
	while(1){
		memset(a,0,sizeof(a));
		a[st] = INF;
		q.push(st);
		while(!q.empty()){
			u = q.front();
			q.pop();
			for(v=1;v<=m;v++){  // 没有连通的点flow[u][v]==cap[u][v]==0
				if(!a[v] && flow[u][v]<cap[u][v]){  //每次a[i]都会被初始化为0,当不为0的时候,就相当于被标记过了
					p[v] = u;
					q.push(v);
					a[v] = min(a[u],cap[u][v]-flow[u][v]);  // u是v的前驱节点,用前驱和当前比较,取小的那一个
				}
			}
		}
		if(a[m]==0) // 找不到增广路,说明已经是最大流
			break;
		sum+=a[m];  //每一次都能找到一条增广路,只有一条哦,所以sum+=a[m],每次只加一次是可行的
		for(i=m;i!=st;i=p[i]){
			flow[p[i]][i] += a[m];
			flow[i][p[i]] -= a[m];  // 将来作比较的时候,用cap[u][v](==0) - flow[u][v](<0) 得到的仍然是一个正数。
									//后向弧为算法纠正自己所犯的错误提供了可能性,它允许算法取消先前的错误的行为
		}
	}
	printf("%d\n",sum);
}

int main(){
	int i,j,k,t,n;
	int st,end,w;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(cap,0,sizeof(cap));
		while(n--){
			scanf("%d%d%d",&st,&end,&w);
			cap[st][end] += w;
		}
		EK(1,m); // 分别是源点和汇点
	}
	return 0;
}




hdu 1532 Drainage Ditches (网络流两种方法)

hdu 1532 Drainage Ditches (网络流两种方法)
  • u012860063
  • u012860063
  • 2014年03月04日 17:54
  • 1895

【网络流之最大流】POJ1273-Drainage Ditche【模板题】

题目链接:http://poj.org/problem?id=1273 这是一道网络流的入门题,用来理解最大流很好。 这个题目我是看的bin神专门为我们这些歌渣渣写的最大流入门博客学的;可以去膜拜一下...
  • wlxsq
  • wlxsq
  • 2015年08月29日 11:21
  • 1422

POJ 1273 Drainage Ditches 我的第一道网络流——最大流问题

题目大意就不说了是一道裸的网络流中的最大流问题,可以用增广路算法来做,即EDMONDS-KARP算法。 读入会有多组数据,每一组先有N和M,N表示图中边的条数,M表示图中节点的个数,接下来有N行数据,...
  • u011401504
  • u011401504
  • 2013年08月12日 11:29
  • 1243

POJ1273 Drainage Ditches(网络流--最大流,EK增广路算法)

Description Every time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite ...
  • riba2534
  • riba2534
  • 2017年12月29日 17:17
  • 88

【poj 1273】Drainage Ditches (最大流裸题)

网络流最大流 【poj 1273】Drainage Ditches (最大流裸题)
  • ALPS233
  • ALPS233
  • 2016年01月31日 20:41
  • 385

POJ 1273 Drainage Ditches(网络流,最大流)

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover...
  • u013582254
  • u013582254
  • 2014年07月27日 09:06
  • 735

poj-1273-Drainage Ditches-一般预流推进算法-最高标号预流推进算法-sap+gap优化

一般预流推进法模版 #include #include #include #include #include using namespace std; #define INF 99999999...
  • rowanhaoa
  • rowanhaoa
  • 2014年04月17日 15:25
  • 2300

【POJ】1273 Drainage Ditches 网络最大流

这是一道经典的网络最大流的题目,个人认为这题入门小白…… 题目大意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水渠中所能流过的水...
  • lyfsb
  • lyfsb
  • 2017年02月16日 10:36
  • 241

POJ 1273 Drainage Ditches 最大流

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54942 ...
  • hyccfy
  • hyccfy
  • 2014年07月17日 20:38
  • 341

网络最大流问题 poj1273 Drainage Ditches

网络最大流问题。 其实想法很直观,就是找一条流的路径,然后取这条路径上的最大流量,然后更新约束条件,然后再找这样的路径。。。直到没有能继续的路径。  找路径的方法使用的是广度优先搜索。  ps:最...
  • mazheng1989
  • mazheng1989
  • 2011年12月07日 10:18
  • 511
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1273 Drainage Ditches
举报原因:
原因补充:

(最多只允许输入30个字)