POJ 1273 Drainage Ditches 网络流入门2
2014-07-14 13:41
169 查看
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
Sample Output
题意:m条通道,n个点,每条通道 含3个数a b c ,c表示通道a到b的最大容量,求该网络的最大流量
代码:
网络流第二种算法 dinic算法的模板 在时间效率上比E-K要优很多。
不过对于dfs函数的递归过程还是不够理解。。
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
题意:m条通道,n个点,每条通道 含3个数a b c ,c表示通道a到b的最大容量,求该网络的最大流量
代码:
#include<iostream> #include<cstdio> #include<queue> using namespace std; int map[222][222]; int h[222]; //分层 int n; int min(int a,int b) { return a<b?a:b; } int bfs() { int z; memset(h,-1,sizeof(h)); queue<int>q; q.push(1); h[1]=0; while(!q.empty()) { z=q.front(); q.pop(); for(int i=1;i<=n;i++) { if(h[i]==-1&&map[z][i]!=0) //寻找后一层 { h[i]=h[z]+1; q.push(i); } } } return h !=-1; } int dfs(int x,int max) { int dt=max; if(x==n) return max; for(int i=1;i<=n;i++) if(map[x][i]!=0&&h[i]==h[x]+1) { int flow=dfs(i,min(dt,map[x][i])); //递归寻找这条路径的最大流 map[x][i]-=flow; map[i][x]+=flow; dt=dt-flow; } return max-dt; } int dinic() { int flow,ans; ans=0; while(bfs()!=0) //每次用BFS分层 { while(flow=dfs(1,1000000)) //求这些层所有增广路的流量 ans=ans+flow; } return ans; } int main() { int a,b,c; int m; while(cin>>m>>n) { memset(map,0,sizeof(map)); while(m--) { cin>>a>>b>>c; map[a][b]+=c; //有重边 所以+=输入 } cout<<dinic()<<endl; } return 0; }
网络流第二种算法 dinic算法的模板 在时间效率上比E-K要优很多。
不过对于dfs函数的递归过程还是不够理解。。
相关文章推荐
- POJ 1273 Drainage Ditches
- 【代码系列】POJ 1273( 单纯形解最大流 )
- poj--1273(最大流基础题)
- poj 1273 Drainage Ditches (网络流 最大流)
- POJ 1273
- poj1273 2010.2.17
- POJ 1273 Drainage Ditches(网络流入门)
- POJ 1273 Drainage Ditches 最大流
- poj 1273 Drainage Ditches
- poj 1273
- POJ 1273 Drainage Ditches
- poj1273解题报告(最大流 EK算法)
- hdu 1532(poj 1273) Drainage Ditches (网络流·最大流)
- poj 1273 Drainage Ditches(最大流,E-K算法)
- poj 1273 || hdu 1532 Drainage Ditches 最大流
- POJ 1273 Drainage Ditches
- POJ-1273 最大流dinic算法
- poj1273
- POJ1273-Drainage Ditches
- USACO 93, POJ 1273 排水沟(Drainage Ditches) 最大流