1. 程式人生 > >HDU1532 Drainage Ditches【網路流 最大流】

HDU1532 Drainage Ditches【網路流 最大流】

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23335    Accepted Submission(s): 11151


 

Problem 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

Source

USACO 93

問題連結:HDU1532 Drainage Ditches

解題思路:最大流模板題。

AC的C++程式:

EdmondsKarp演算法(鄰接矩陣形式)

#include<iostream>
#include<cstring>
#include<algorithm> 
#include<queue>

using namespace std;

const int N=205;
const int INF=0x3f3f3f3f;

int g[N][N];
int pre[N];//路徑上每個結點的前驅結點
bool vis[N];
int n,m;//n是頂點數,m是邊數。頂點從1開始編號,1是源,n是匯

int EdmondsKarp()
{
	int i,v;
	queue<int>q;
	memset(pre,-1,sizeof(pre));
	memset(vis,false,sizeof(vis));
	pre[1]=0;
	vis[1]=true;
	q.push(1);
	bool flag=false;//標記bfs是否尋找到一條從源到匯的可行路徑
	while(!q.empty())
	{
		v=q.front();
		q.pop();
		for(int i=1;i<=m;i++)
		  if(g[v][i]>0&&vis[i]==0)//必須是依然後容量的邊,才可以走 
		  {
		  	 pre[i]=v;
		  	 vis[i]=true;
		  	 if(i==n)
		  	 {
		  	 	flag=true;
		  	 	break;
			 }
			 else
			   q.push(i);
		  }
	} 
	if(!flag)//找不到就退出 
	  return 0; 
	int ans=INF;
	v=n;
	//尋找源到匯路徑上容量最小的邊,其容量就是此次增加的總流量
	while(pre[v])
	{
		ans=min(ans,g[pre[v]][v]);
		v=pre[v];
	}
	//沿此路徑新增反向邊,同時修改路徑上每條邊的容量
	v=n;
	while(pre[v])
	{
		g[pre[v]][v]-=ans;
		g[v][pre[v]]+=ans;
		v=pre[v];
	}
	return ans;
} 

int main()
{
	while(~scanf("%d%d",&m,&n))//m是邊數,n是頂點數 
	{
		int s,e,c;
		memset(g,0,sizeof(g));
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&s,&e,&c);
			g[s][e]+=c;
		}
		int ans=0,temp;
		while(temp=EdmondsKarp())
		  ans+=temp;
		printf("%d\n",ans);
	}
	return 0;
}

 

Dinic演算法(鄰接矩陣形式)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int N=205;
const int INF=0x3f3f3f3f;

int g[N][N],layer[N];
bool vis[N];
int n,m;//1是源點 n是匯點


bool CounterLayer()
{
	queue<int>q;
	memset(layer,-1,sizeof(layer));
	layer[1]=0;
	q.push(1);
	while(!q.empty())
	{
		int v=q.front();
		q.pop();
		for(int i=1;i<=n;i++)
		  if(g[v][i]>0&&layer[i]==-1)
		  {
		  	 layer[i]=layer[v]+1;
		  	 if(i==n)//分層到匯點即可 
		  	   return true;
		  	 else
		  	   q.push(i);
		  }
	}
	return false;
} 

int Dinic()
{
	int ans=0;
	deque<int>q;//DFS用的棧 
	while(CounterLayer())//只要能分層 
	{
		q.push_back(1);//源點入棧
		memset(vis,false,sizeof(vis));
		vis[1]=true;
		while(!q.empty())
		{
			int v=q.back();
			if(v==n)//如果v是匯點,在棧中找容量最小邊 
			{
				int temp=INF;
				int u;//容量最小邊的起點
				for(int i=1;i<q.size();i++)
				{
					int vs=q[i-1];
					int ve=q[i];
					if(g[vs][ve]>0&&temp>g[vs][ve])
					  temp=g[u=vs][ve];
				}
				//改圖
				ans+=temp;
				for(int i=1;i<q.size();i++)
				{
					int vs=q[i-1];
					int ve=q[i];
					g[vs][ve]-=temp;
					g[ve][vs]+=temp;
				}
				//退棧到temp成為棧頂,以便繼續dfs
				while(!q.empty()&&q.back()!=u)
				{
					vis[q.back()]=false;
					q.pop_back();
				} 
			}
			
			else//如果v不是匯點 
			{
				int i;
				for(i=1;i<=n;i++)//只往下一層的沒有走過的結點走
				  if(g[v][i]>0&&layer[i]==layer[v]+1&&!vis[i])
				  {
				  	  vis[i]=true;
					  q.push_back(i);
					  break;	
				  }
				if(i>n)//找不到下一個結點
				  q.pop_back(); 
			}
		 } 
	}
	return ans;
}

int main()
{
	while(~scanf("%d%d",&m,&n))
	{
		int s,e,c;
		memset(g,0,sizeof(g));
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&s,&e,&c);
			g[s][e]+=c;
		}
		printf("%d\n",Dinic());
	}
	return 0;
}