POJ---1273 Drainage Ditches[EK()]
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41645 | Accepted: 15583 |
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.
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
code:
1 #include <iostream> 2 #include <iomanip> 3 #include <fstream> 4 #include <sstream> 5 #include <algorithm> 6 #include <string> 7 #include <set> 8 #include <utility> 9 #include <queue> 10 #include <stack> 11 #include <list> 12 #include <vector> 13 #include <cstdio> 14 #include <cstdlib> 15 #include <cstring> 16 #include <cmath> 17 #include <ctime> 18 #include <ctype.h> 19 using namespace std; 20 21 #define MAXN 500 22 #define intmax 99999999 23 24 int cap[MAXN][MAXN]; 25 int flow[MAXN][MAXN]; 26 int a[MAXN]; 27 int pre[MAXN]; 28 int n,m; 29 30 31 int EK() 32 { 33 queue<int>Que; 34 int f=0; 35 memset(flow,0,sizeof(flow)); 36 for(;;) 37 { 38 memset(a,0,sizeof(a)); 39 a[1]=intmax; 40 Que.push(1); 41 while(!Que.empty()) 42 { 43 int u=Que.front(); 44 Que.pop(); 45 for(int i=1;i<=m;i++) 46 if(!a[i]&&cap[u][i]>flow[u][i]) 47 { 48 pre[i]=u; 49 Que.push(i); 50 a[i]=a[u]>cap[u][i]-flow[u][i]?cap[u][i]-flow[u][i]:a[u]; 51 } 52 } 53 if(a[m]==0) 54 break; 55 for(int i=m;i!=1;i=pre[i]) 56 { 57 flow[pre[i]][i]+=a[m]; 58 flow[i][pre[i]]-=a[m]; 59 } 60 f+=a[m]; 61 } 62 return f; 63 } 64 65 int main() 66 { 67 int a,b,temp; 68 while(~scanf("%d%d",&n,&m)) 69 { 70 memset(cap,0,sizeof(cap)); 71 for(int i=0;i<n;i++) 72 { 73 scanf("%d%d%d",&a,&b,&temp); 74 cap[a][b]+=temp; 75 } 76 printf("%d\n",EK()); 77 } 78 return 0; 79 }
If you have any questions about this article, welcome to leave a message on the message board.
Brad(Bowen) Xu
E-Mail : maxxbw1992@gmail.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步