HDU-1532 Drainage Ditches(网络流量)

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

翻译

农民约翰修建了一套排水沟,还在每条沟渠的开头安装了调节器,这样他就可以控制水流入沟渠的速度。农民约翰不仅知道每条沟渠每分钟能输送多少加仑的水,还知道沟渠的确切布局,这些沟渠从池塘中流出,相互流入,形成一个潜在的复杂网络。
根据所有这些信息,确定水从池塘输送到溪流的最大速率。对于任何给定的沟渠,水只朝一个方向流动,但可能有一种方式可以使水绕一圈流动。
输入
输入包括几个案例。对于每种情况,第一行包含两个空格分隔的整数,N(0<=N<=200)和M(2<=M<=200)。N是农民约翰挖的沟的数量。M是这些沟渠的交点数。1号路口是池塘。交点M是河流。以下N行中的每一行都包含三个整数:Si、Ei和Ci。Si和Ei(1<=Si,Ei<=M)表示沟渠流动的交叉点。水将通过这条沟渠从Si流向Ei。Ci(0<=Ci<=10000000)是水流通过沟渠的最大速率。
输出
对于每种情况,输出一个整数,即从池塘中排空水的最大速率。

代码

//博客 
#include "stdio.h"
#include "string.h"
#include "queue"
#include "algorithm"
#include "math.h"
using namespace std;
int pri[202][202],g[202][202],p[202][202];
int vis[202],ff[202];
int n,m;
int EK(int s,int t)
{
    
    
	memset(ff,0,sizeof(ff));
	memset(vis,0,sizeof(vis));
	vis[s]=1;
	queue<int>q;
	q.push(s);
	while(!q.empty())
	{
    
    
		int k=q.front();
		q.pop();
		for(int i=1; i<=m; i++)
		{
    
    
			if(vis[i]==0&&g[k][i]>0)
			{
    
    
				vis[i]=1;
				ff[i]=k;
				if(i==t)
					return 1;
				q.push(i);
			}
		}
	}
	return 0;
}
int ppp(int s,int t)
{
    
    
	int sum=0;
	while(EK(s,t))
	{
    
    
		int v=t;
		int n1,n2;
		int ss=199999999;
		while(v!=s)
		{
    
    
			n1=ff[v];
			ss=min(ss,g[n1][v]);
			v=n1;
		}
		sum+=ss;
		v=t;
		while(v!=s)
		{
    
    
			n1=ff[v];
			g[n1][v]-=ss;
			g[v][n1]+=ss;//懂:建立 反边 :我们有反悔的机会  下面解释 
			v=n1;
		}
	}
	return sum;
}
int main()
{
    
    
	int i,j,k,l;
	while(~scanf("%d%d",&n,&m))
	{
    
    
		memset(g,0,sizeof(g));
		for(i=0; i<n; i++)
		{
    
    
			scanf("%d%d%d",&j,&l,&k);
			g[j][l]+=k;
		}
		printf("%d\n",ppp(1,m));
	}
}
/*			
	若此处不清楚,可再看下面,下面还有,都是一样的,我只是觉得放在代码里格式好看些
		a-->b  b-->d a-->c c-->d b-->d 流量都为1 
		求a-->d最大流量:
		a-->b-->d
		a-->c-->d	
		最大为2;
		假设我们一开始走错了,走成
		a-->b-->c-->d  此时没有反边,则这样算最大流量为 1;答案错误
		建立反边  d-->c
				  c-->b
				  b-->a
				 则此时我们又有一条路 a-->c-->b-->d  
		即 a-->b-->c-->d
		a-->c-->b-->d  相当于b-->c和c-->b 这条路我们没走 
*/		 	

解释

a–>b b–>d a–>c c–>d b–>d 流量都为1
求a–>d最大流量:
a–>b–>d
a–>c–>d
最大为2;
假设我们一开始走错了,走成
a–>b–>c–>d 此时没有反边,则这样算最大流量为 1;答案错误
建立反边
d–>c
c–>b
b–>a
则此时我们又有一条路 a–>c–>b–>d
即 我们有两条路
a–>b–>c–>d
a–>c–>b–>d
相当于b–>c和c–>b 这条路我们没走
答案为2
注:建立反边 :我们就有反悔的机会

猜你喜欢

转载自blog.csdn.net/weixin_53623850/article/details/120874011
今日推荐