HDU 1532 Drainage Ditches
2015-08-11 14:58
309 查看
Drainage Ditches
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
[align=left]Sample Input[/align]
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
[align=left]Sample Output[/align]
50
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct edge { int from,to,cap,flow; edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}; }; const int inf=999999; int m,n; int a[205]; int p[205]; vector<int>G[405]; vector<edge>edges; void init() { for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void addedge(int from,int to,int cap) { edges.push_back(edge(from,to,cap,0)); edges.push_back(edge(to,from,0,0)); int m=edges.size(); G[to].push_back(m-1); G[from].push_back(m-2); } int maxflow(int s,int t) { int flow=0; while(1) { memset(a,0,sizeof(a)); queue<int>q; q.push(s); a[s]=inf; while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0;i<G[x].size();i++) { edge& e=edges[G[x][i]]; if(!a[e.to]&&e.cap>e.flow) { p[e.to]=G[x][i]; a[e.to]=min(a[x],e.cap-e.flow); q.push(e.to); } } if(a[t])break; } if(!a[t])break; for(int u=t;u!=s;u=edges[p[u]].from) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; } flow+=a[t]; } return flow; } int main() { int a,b,c; while(scanf("%d%d",&m,&n)!=EOF) { init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); } int ans=maxflow(1,n); printf("%d\n",ans); } return 0; }
相关文章推荐
- SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender
- MDK __main过程分析
- 树形DP - hdu5293 Tree chain problem
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
- Binder与AIDL服务
- **深入浅出的事件路由(view hit test)**
- leetCode #217 Contains Duplicate
- HDU 4819 Mosaic D区段树
- sleep和wait区别
- windows 2012 R2 DHCP FailOver
- commons-mail报错,提示EmailException: Sending the email to the following server failed
- 不依赖sendmail和postfix发邮件
- HDU 1082 Matrix Chain Multiplication
- 《实体解析与信息质量》 - 3.1.3 Algebraic模型
- mdadm彻底删除software RAID
- GenyMotion报错An error occured while deploying a file.INSTALL_FAILED_INVALID_URI
- UVa 12878 - Flowery Trails(最短路应用)
- Air Raid
- saiku安装方法总结
- Understand the Business Domain