poj 1273 Drainage Ditches(最大费用流+最短增广路径算法)
2013-03-03 14:05
405 查看
Drainage Ditches
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
Source
USACO 93
思路:网络流入门,建图无难度。我用的是最短增广路径算法。复杂度是O(n*m*m)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 45239 | Accepted: 17002 |
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
Source
USACO 93
思路:网络流入门,建图无难度。我用的是最短增广路径算法。复杂度是O(n*m*m)
#include<iostream> #include<cstring> using namespace std; const int mm=210; const int oo=1e9; class node { public:int c,f; node(){c=f=0;} }e[mm][mm]; int pre[mm],le[mm][mm],q[mm]; int m,n,s,t,a,b,c; void path_add()///找增广路径 { memset(le,0,sizeof(le)); memset(pre,-1,sizeof(pre)); pre[1]=-2;int qs=0,qt=1;q[qs]=1; int z; while(qs!=qt&&pre[t]==-1) { z=q[qs++];qs%=mm; for(int i=1;i<=n;i++) { if(pre[i]==-1) { if(e[z][i].c-e[z][i].f>0)///le[][]为残留网络 le[z][i]=e[z][i].c-e[z][i].f,pre[i]=z,q[qt++]=i,qt%=mm; else if(e[i][z].f>0) le[z][i]=e[i][z].f,pre[i]=z,q[qt++]=i,qt%=mm; } } } } int flow_go() { if(pre[t]==-1)return 0; int ret=oo,u,v; for(u=pre[t],v=t;u!=-2;v=u,u=pre[u])///找出最小流 { if(ret>le[u][v])ret=le[u][v]; } if(ret>0) { u=pre[t];v=t; while(u!=-2)///更改网络 { if(e[u][v].c-e[u][v].f>0) e[u][v].f+=ret; else if(e[v][u].f>0) e[u][v].f+=ret; v=u;u=pre[u]; } return ret; } else return 0; } int flow_max() { int ret=0,z; while(1) { path_add(); z=flow_go(); ret+=z; if(z==0)return ret; } } int main() { while(cin>>m>>n) { memset(e,0,sizeof(e)); for(int i=0;i<m;i++) { cin>>a>>b>>c; e[a][b].c+=c; }s=1;t=n; cout<<flow_max()<<"\n"; } }
相关文章推荐
- poj 1273 Drainage Ditches(最大费用流+最短增广路径算法)
- poj 1273 最大流之最短路径增广法(EK)
- 最大流的算法——Edmonds-Karp算法(最短路径增广算法)
- 初级->图算法->最短路径 poj 1062 昂贵的聘礼
- POJ 1273-Drainage Ditches(网络流_最大流_ISAP()算法和EK()算法)
- [POJ](2139)Six Degrees of Cowvin Bacon ---- 最短路径算法(Floyd)
- HDU 3549 Flow Problem(最大流最短路径增广)
- poj3020 匈牙利算法+公式:二分无向图的最小路径覆盖 = 顶点数 - 最大二分匹配数 / 2
- POJ-2387 Til the Cows Come Home(Dijsktra算法求最短路径)
- POJ1273 Drainage Ditches【最大流、增广路算法Edmonds_Karp】
- 初级->图算法->最短路径 poj 2253 Frogger
- POJ 3169 SPFA 差分约束 最大值->最短路径求法
- POJ 1860 Currency Exchange Bellman-Ford算法求单源最短路径并判断是否有正权回路
- 初级->图算法->最短路径 poj 1860 Currency Exchange
- isap算法模板poj 1273gap+弧优化 最大流
- isap算法模板poj 1273gap+弧优化 最大流
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
- POJ 3835 Columbus's bargain (最短路径 spfa 算法)
- 最大流最小割算法; BFS搜索增广路径; 算法简单,打印结果也比较清晰;
- 初级->图算法->最短路径 poj 3259 Wormholes