HDU 1532 Drainage Ditches 最大流模板题

版权声明:本文原创如果喜欢,欢迎转载。^_^ https://blog.csdn.net/ccutyear/article/details/68214982

Drainage Ditches

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


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

题意:编号为1的地方是一个大水塘(源点),n是一个溪流(汇点)。有n条连接两个点的管道。每条管道的没秒的最大流量是Ci。问:没秒最多可以有多少水量从大水塘流入溪流。


思路:只要明白题意后很容易就会想到这是一个最大流题。今天是我练习最大流的第一天,才刚学会最大流的Ford-Fullkerson算法,而且我现在只会用DFS实现。在接下的练习中每当我学会一个新的最大流算法(至少还有Edmond-Karp算法)或者实现方法(至少还有BFS实现的)。我会返回来更新博客。


下面是DFS实现:

#include<iostream>
#include<cstring>
#include<cmath>

#define MAX 205
#define INF 0x3f3f3f3f
using namespace std;
int m,n;
int Map[MAX][MAX];
bool used[MAX];
int DFS(int u,int en,int f)
{
	if(u == en) return f;
	used[u] = true;
	for(int v = 1; v <= m; v++){ 
		if(Map[u][v] > 0 && !used[v]){
			int d = DFS(v,en,min(Map[u][v],f));
			if(d > 0){
				Map[u][v] -= d; //u->v 是一条正向边。 
				Map[v][u] += d; //v->u 是一条反向边。  
				return d;
			}
		}
	}
	return 0;
}
int MaxFlow(int st,int en)
{
	int flow = 0;
	while(true){
		memset(used,false,sizeof(used));
		int f = DFS(st,en,INF);
		if(f == 0) break;
		flow += f;
	}
	return flow;
}
int main( )
{
	while(cin >> n >> m){
		memset(Map,0,sizeof(Map));
		while(n--){  //开始存图 
			int a,b,dis;
			cin >> a >> b >> dis;
			Map[a][b] += dis;//因为数据量小所以使用邻接矩阵也行。 
/*刚开始的时候没考虑重边的情况后来想到重边的情况。
之后想到改成了Map[a][b] = max(Map[a][b],dis),最后
想到,如果有两条从a->b的边,应该是相加而不是覆盖。*/
		}
		cout << MaxFlow(1,m) << endl;
	}
}


想法:在AC之后我就上网查了其他人的代码,看到他们大都是使用邻接表存储,而且用的是BFS写法,没看懂........在最大流练完之后那些代码我自己应该就能写了吧!



扫描二维码关注公众号,回复: 3049526 查看本文章






猜你喜欢

转载自blog.csdn.net/ccutyear/article/details/68214982
今日推荐