hdu 1532:Drainage Ditches
Posted HWIM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1532:Drainage Ditches相关的知识,希望对你有一定的参考价值。
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18648 Accepted Submission(s):
8845
Problem 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
code
题目多组测试数据!!!
每组数据的初始化变量数组!!!
n才是边!!!
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 6 using namespace std; 7 8 const int INF = 100000; 9 const int MAXN = 2100; 10 struct Edge{ 11 int to,nxt,c; 12 }e[MAXN]; 13 int head[MAXN],dis[MAXN]; 14 int tot,s,t; 15 queue<int>q; 16 17 inline int read() 18 { 19 int x = 0,f = 1;char ch = getchar(); 20 for (; ch<‘0‘||ch>‘9‘; ch = getchar()) 21 if (ch==‘-‘) f = -1; 22 for (; ch>=‘0‘&&ch<=‘9‘; ch = getchar()) 23 x = x*10+ch-‘0‘; 24 return x*f; 25 } 26 inline void add_edge(int u,int v,int w) 27 { 28 ++tot; 29 e[tot].to = v,e[tot].nxt = head[u],e[tot].c = w; 30 head[u] = tot; 31 ++tot; 32 e[tot].to = u,e[tot].nxt = head[v],e[tot].c = 0; 33 head[v] = tot; 34 } 35 inline bool bfs() 36 { 37 memset(dis,-1,sizeof(dis)); 38 q.push(s); 39 dis[s] = 0; 40 while (!q.empty()) 41 { 42 int u = q.front(); 43 q.pop(); 44 for (int i=head[u]; i; i=e[i].nxt) 45 { 46 int v = e[i].to; 47 if (dis[v]==-1 && e[i].c>0) 48 { 49 dis[v] = dis[u]+1; 50 q.push(v); 51 } 52 } 53 } 54 if (dis[t]!=-1) return true; 55 return false; 56 } 57 int dfs(int u,int cp) 58 { 59 if (u==t) return cp; 60 int w,tmp = cp; 61 for (int i=head[u]; i; i=e[i].nxt) 62 { 63 int v = e[i].to; 64 if (dis[v]==dis[u]+1 && e[i].c>0) 65 { 66 w = dfs(v,min(cp,e[i].c)); 67 e[i].c -= w; 68 e[i^1].c += w; 69 cp -= w; 70 } 71 } 72 return tmp-cp; 73 } 74 int main() 75 { 76 int n,m,ans; 77 while (scanf("%d%d",&n,&m)!=EOF) 78 { 79 memset(head,0,sizeof(head));//初始化 80 tot = 1; 81 s = 1,t = m; 82 for (int x,y,z,i=1; i<=n; ++i) 83 { 84 x = read(),y = read(),z = read(); 85 add_edge(x,y,z); 86 } 87 ans = 0; 88 while (bfs()) 89 ans += dfs(s,INF); 90 printf("%d\n",ans); 91 } 92 return 0; 93 }
以上是关于hdu 1532:Drainage Ditches的主要内容,如果未能解决你的问题,请参考以下文章
HDU1532 Drainage Ditches (网络流)