天天看点

POJ1273 Drainage Ditches 网络最大流

网络最大流。 Drainage Ditches

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 41550 Accepted: 15536

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<algorithm>
#include<cmath>

using namespace std;

#define MAXN 220
#define INF 0xFFFFFF

struct node
{
	int c,f;
};

node edge[MAXN][MAXN];   //矩阵 存储弧 
int flag[MAXN]; //顶点状态 -1 未标记  0 标记但未检查 1 标记并检查
int pre[MAXN];  //指明标号从那里得到
int a[MAXN];   //改进量
int que[MAXN];//队列 
int n,m;

//一般的Ford-Fulkerson 
void maxflow(int n,int s,int t)
{
	int start,end;  //队头队尾 
	int tag; 
	while(1)  
	{
		memset(flag,-1,sizeof(flag)); 
		memset(pre,-1,sizeof(pre));
		memset(a,-1,sizeof(a));
		
		flag[1]=pre[1]=0;
		start=end=1 ,a[1]=INF;
		que[end++]=1;
		while(start<end && flag[n]==-1)
		{
			tag=que[start++];
			for(int i=1;i<=n;i++)
			{
				if(flag[i]==-1)
				{
					//正向,未满 
					if(edge[tag][i].c<INF && edge[tag][i].f<edge[tag][i].c)
					{
						flag[i]=0,pre[i]=tag;
						a[i]=min(a[tag],edge[tag][i].c-edge[tag][i].f);
						que[end++]=i;
					}
					else
						//负向,存在流量 
						if(edge[i][tag].c<INF && edge[i][tag].f>0)
						{
							flag[i]=0,pre[i]=-tag;
							a[i]=min(a[tag],edge[i][tag].f);
							que[end++]=i;
						}
				}
			}
			flag[tag]=1;
		}
		//终点没有获得编号,或者终点调整量为0,则退出当前循环。 
		if(flag[n]==-1 || a[n]==0 )  break;
		int k1=n,k2=abs(pre[n]); 
		int plus=a[n];
		while(1)
		{
			if(edge[k2][k1].f<INF)
				edge[k2][k1].f+=plus;
			else
				edge[k1][k2].f-=plus;
			if(k2==1)
				break;
			k1=k2,k2=abs(pre[k2]);
				
		}	
	}
	
	int max=0;
	for(int i=1;i<=n;i++)
	{ 
		if(edge[1][i].f<INF) 
			max+=edge[1][i].f;
	} 
	cout<<max<<endl;
	
} 

void init()
{
	int a,b,c;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			edge[i][j].c=edge[i][j].f=INF;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		if(edge[a][b].f<INF)
		{ 
			edge[a][b].c+=c;
			continue; //题目有重边 
		} 
		edge[a][b].c=c;
		edge[a][b].f=0;
	}
}

int main()
{
	while(cin>>m>>n)
	{
		init();
		maxflow(n,1,n); 
	} 
	return 0; 
} 
           

继续阅读