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;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj1273&hdu1523Drainage Ditches(ISAP)

题目请戳这里 题目大意:略。 题目分析:网络流模版题。不过数据很弱,只能测很烂的模版。 第一道网络流 详情请见代码: #include #include #include #include...

POJ-1273 Drainage Ditches【最大流】

题目链接:http://poj.org/problem?id=1273 题目大意: 有一个水塘,一个大海,要把水塘里的水尽可能多的排到大海里。当然,要通过一些水渠,这些水渠有一定的容量限制。 问...

POJ-1273-Drainage Ditches(最大流)dinic实现 后续模板待补充

Sample Input 5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10 Sample Output 50 #include #include #i...

poj 1273 Drainage Ditches 最大流问题.

Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68476 Accep...

POJ 1273 Drainage Ditches——最大流

题目传送门 这道题就是裸的最大流,因为各路大神强烈推荐Dinic算法,所以我舍弃EK算法。Dinic 算法Dinic也是一种增广路算法,通过对残量网络进行分层之后用DFS求增广路直到无法增广。 层...

poj 1273 Drainage Ditches (最大流Dinic)

题目链接:   1273 题目大意:   有N个点和M条边,每条边最大的流量为c,初始流量为0                   1为源点,n为汇点求最大流 解题思路...

POJ1273 Drainage Ditches——最大流

经典网络流基础题,刚刚接触网络流的同学可以体会一下网络流的魅力。 推荐学习网络流的教材:向期中向总等湖南金牌教练们主编的《奥赛经典·提高篇》 给出关键部分伪代码来介绍一下网络流的标号法: Rep...

POJ1273 Drainage Ditches(裸最大流,EK,DINIC)

注意重边。 EK: #include #include #include #define min(a,b) a<b?a:b using namespace std; const int N...

poj——1273 Drainage Ditches(费用流模版)

DescriptionEvery time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover p...

poj 1273 Drainage Ditches 网络流 Edmond Karp || Dinic

// poj 1273 Drainage Ditches 网络流 Edmond Karp || Dinic // 题意: 裸的网络流,点数200,边数200 // 解答:经典的ff算法由于效率受...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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