POJ 1273 最大流

来源:互联网 发布:淘宝开店及投资 编辑:程序博客网 时间:2024/05/01 03:50

题目:

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.

 

题目大意:是说John有个池塘,每到下雨时节池塘里会被水草覆盖,附近有条河,于是他想挖沟渠放水,将水草清除掉。给你一组数据,数据包括沟渠的数目和每条沟渠的最大容量(及水的最大流量),还有沟渠的交汇点的数目。要你按照数据给的值求出从池塘流向河的最大流量。

注意:

(1)在题目的input中第一句话是黑体的,也就是说这句话很重要,及“The input includes several cases”,意思是要输入几组数据,但在输入里并没有体现到底要输入几组数据,解决方法在代码中会提到。

(2)题目给的数据会出现重边。(重点注意哦)

代码:

#include<iostream>#include<queue>using namespace std;int maximum[202][202];//边int head[202];//记录访问过的节点的前驱结点int flow[202];//可更新容量int stard,end;//起点,终点int n,m;//边数和节点数queue<int>q;int bfs()//用广搜搜索增广路{while(!q.empty())q.pop();//清空队列(保持程序的严谨性,也可以在函数中定义局部变量,就不用清空了)int i,t;for(i=2;i<=m;i++)head[i]=-1;//初始化head数组head[1]=0;flow[1]=0x7fffffff;q.push(stard);while(!q.empty()){i=q.front();q.pop();if(i==end)break;for(t=1;t<=m;t++){if(t!=stard&&head[t]==-1&&maximum[i][t])//没访问过且有则边可以选取{flow[t]=flow[i]<maximum[i][t]?flow[i]:maximum[i][t];//选择最小的可更新容量head[t]=i;//记录前驱结点q.push(t);}}}if(head[end]==-1)return -1;//如果终点没访问则无增广路return flow[end];}void f(){int now,step,max_flow=0,i;while((step=bfs())!=-1){max_flow+=step;//计算最大流now=end;while(now!=stard)//更新边的容量{i=head[now];maximum[i][now]-=step;maximum[now][i]+=step;//增加反向边now=i;}}cout<<max_flow<<endl;}int main(){int i,j,k,w;while(scanf("%d %d",&n,&m)!=EOF)//解决输入不定组数数据的方法(重点注意){//初始化边for(i=1;i<=m;i++)for(j=i;j<=m;j++)maximum[j][i]=maximum[i][j]=0;for(k=0;k<n;k++){cin>>i>>j>>w;maximum[i][j]+=w;//重点,防止重边的出现}stard=1;end=m;f();}return 0;}


 

原创粉丝点击