poj1273 Drainage Ditches(裸最大流)

Drainage Ditches

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10 

Sample Output

 50    

思路:单源单汇最大流问题,这次用到了残余网络,每次找到一条增广路,min里存的是所有相连边的最小值,不是这条增广路上的最小值,然后此条增广路

上的每条边减去最小值,再重新开始找增广路,重复执行以上操作,直到没有增广路为止。

1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int cap[205][205],pre[205];//cap 残量(残余网络),n 汇点
 7 int n;
 8 queue<int>p;
 9 
10 int _min(int a,int b)
11 {
12     return a<b?a:b;
13 }
14 
15 void bfs()
16 {
17     int Maxflow = 0, i;
18     while(1)
19     {
20         pre[1]=1;
21         p.push(1);
22         int min=999999999;
23         memset(pre,0,sizeof(pre));
24         while(!p.empty())
25         {
26             int u=p.front();
27             p.pop();
28             for(i=1;i<=n;i++)
29             {
30                 if(pre[i]==0  && cap[u][i]>0)
31                 {
32                     pre[i]=u;
33                     p.push(i);
34                     min=_min(min,cap[u][i]);//找到此时遍历到的所有边的最短值,并不是增广路上的最小容量值
35                 }
36             }
37         }
38         if(pre[n]==0)  break;
39 
40         for(int s=n; s!=1; s=pre[s]){
41             cap[pre[s]][s]-= min;
42             cap[s][pre[s]]+= min;
43         }
44         Maxflow += min;
45     }
46     printf("%d\n", Maxflow);
47 }
48 
49 int main()
50 {
51     int m, a, b, c, i;
52     while(scanf("%d%d",&m,&n)!=EOF)
53     {
54         memset(cap,0,sizeof(cap));
55         for(i=0;i<m;i++)
56         {
57             scanf("%d%d%d",&a,&b,&c);
58             cap[a][b]+=c;
59         }
60         bfs();
61     }
62     return 0;
63 }


poj1273 Drainage Ditches(裸最大流),布布扣,bubuko.com

时间: 08-20

poj1273 Drainage Ditches(裸最大流)的相关文章

POJ-1273 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

POJ1273 Drainage Ditches 【最大流Dinic】

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56870   Accepted: 21863 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

[POJ1273]Drainage Ditches 网络流(最大流)

题目链接:http://poj.org/problem?id=1273 网络流裸题,注意有重边.重边的处理方法很简单,就是将对应的c叠加到对应边上.注意初始化为0. 我用的是最朴素的FF方法,即找增广路.之前用dfs找增广路WA了,应该是碰到了随机找一条增光路这种方法碰到了killer case.给出WA代码(初学用喜闻乐见链式前向星建图,比较繁琐.不过好在最终还是WA掉了). 1 #include <algorithm> 2 #include <iostream> 3 #incl

POJ1273 Drainage Ditches【最大流】【SAP】

题目链接: http://poj.org/problem?id=1273 题目大意: 农民John的田里有M个池塘和N条水沟用来排水,池塘编号为1~M,1号池塘是所有水沟的源点, M号池塘是水沟的汇点.给你N条水沟所连接的池塘和所能流过的水量,求整个水沟从源点到汇点 最多能流多少水. 思路: 很明显的求网络流最大流问题.用链式前向星(邻接表)来存储网络,这样就不用考虑重边问题了.这 里的重边其实就是平行边.用SAP算法+GAP优化来求最大流就可以了.SAP+GAP模板参考我的另 一篇博文:htt

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

POJ 1273 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 drain

Drainage Ditches(最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64044   Accepted: 24718 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