首页 > 代码库 > Drainage Ditches(最大流基础_增广路算法)
Drainage Ditches(最大流基础_增广路算法)
Drainage DitchesCrawling in process...Crawling failedTime Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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.
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
、
题意:有n条路,m个点,让你求出以1为源点,m为汇点的最大流;
思路:典型的最大流增光路的算法。当且仅当残量网络中不存在s-t有向道路(增广路)时,此时的流是从s到t的最大流;
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include <queue> using namespace std; #define inf 9999999999 int flow[210][210]; int maxflow[210],father[210],vis[210]; int max_flow; int m,i; void EK(int s,int e) { queue<int >q; int u,v; while(1) { memset(maxflow,0,sizeof(maxflow));//每次寻找增广路径都将每个点的流入容量置为0; memset(vis,0,sizeof(vis)); maxflow[s]=inf;//源点的流入量置为正无穷; q.push(s);//将源点压入队列; while(!q.empty())//当队列不为空 { u=q.front(); q.pop(); for(v=s;v<=e;v++) { if(!vis[v]&&flow[u][v]>0) { vis[v]=1; father[v]=u;//记录下他的父亲方便反向更新 q.push(v); maxflow[v]=min(maxflow[u],flow[u][v]);//当前点的容量为父亲点容量与边流量的较小者 } } if(maxflow[e]>0)//如果找到了汇点并且汇点容量不为0则清空队列 { while(!q.empty()) q.pop(); break; } } if(maxflow[e]==0)//已经找不到到汇点的增光路经了,就退出整个循环 break; for(i=e;i!=s;i=father[i]) { flow[father[i]][i]-=maxflow[e];//正向更新 flow[i][father[i]]+=maxflow[e];//反向更新 } max_flow+=maxflow[e];//更新最大流 } } int main() { int n; int si,ei,ci; while(~scanf("%d %d",&n,&m)) { max_flow=0;//最大流初始化; memset(flow,0,sizeof(flow)); for(i=0;i<n;i++) { scanf("%d %d %d",&si,&ei,&ci); flow[si][ei]+=ci; } EK(1,m); printf("%d\n",max_flow); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。