poj 1273-小白算法练习 Drainage Ditches 网络流

来源:互联网 发布:2016淘宝打假新规则 编辑:程序博客网 时间:2024/06/05 02:12
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 76082 Accepted: 29559

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 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50

翻译:

为了某样植物在池塘被水淹,john装了排水沟,让池塘水流进河里,并在沟上安装了**来控制排水的速度。给出所有信息,决定水流进河里的最大速率,(沟流水的方向只能有一个,但可以是环)

输入:

第 一  行:N (沟数量)   M (沟与沟之间交叉点的数量)

后面N行:Si     Ei    Ci    (Si 流向Ei  [ 1代表池塘,M代表河 ],Ci是速度)

输出:

水流出的最大速率

思路:

下面是张图,对应上面的sample。上面的数字代表这最大的流量,也就是容量。最大流的问题。我在前面转了一条博客,可以去看一下


代码:

#include<iostream>#include<algorithm>#include<cstring>#include<queue> using namespace std;const int MAX=201;int moreflow[MAX][MAX]; int pre[MAX];//前驱结点 为了找到增光路的瓶颈边int n,m;//代表图中有多少边和节点 bool BFS(int start,int end){int vis[MAX];memset(pre,-1,sizeof(pre));memset(vis,0,sizeof(vis));queue<int>que;//初始化必要的参数 que.push(start);vis[start]=1;while(!que.empty()){int T=que.front();if(T==end) return true;else{que.pop();for(int i=1;i<=m;i++){if(vis[i]==0 && moreflow[T][i]>0){vis[i]=1;pre[i]=T;que.push(i);}}}} return false;}int max_flow(int start,int end){int maxflow=0;int thisminflow;int cur;while(BFS(start,end)){cur=end;thisminflow=INT_MAX;while(pre[cur]!=-1){thisminflow=min(thisminflow,moreflow[pre[cur]][cur]);cur=pre[cur];} //找到瓶颈边 maxflow+=thisminflow;cur=end;while(pre[cur]!=-1){moreflow[pre[cur]][cur]-=thisminflow;moreflow[cur][pre[cur]]+=thisminflow;cur=pre[cur];} //更新残余网络 }return maxflow;}  int main(){ while(cin>>n>>m) {int start=0,end=0,weight=0;memset(moreflow,0,sizeof(moreflow));for(int i=0;i<n;i++){cin>>start>>end>>weight;moreflow[start][end]+=weight;}//初始化残余网络cout<<max_flow(1,m)<<endl; } return 0;}

原创粉丝点击