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

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/84714887