HDU1532--Drainage Ditches
来源:互联网 发布:如何不越狱下载软件 编辑:程序博客网 时间:2024/05/17 17:17
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;#define maxn 508#define inf 0x3f3f3f3fint dis[maxn],first[maxn],nxt[maxn];int vv[maxn],cost[maxn];int e = 1;inline int min(int a,int b) { return a>b?b:a;}void add(int u,int v,int c){ vv[e] = v;cost[e] = c;nxt[e] = first[u];first[u] = e++; vv[e] = u;cost[e] = 0;nxt[e] = first[v];first[v] = e++;}bool bfs(int s,int t){ memset(dis,-1,sizeof(dis)); queue <int> q; q.push(s); dis[s] = 0; while(!q.empty()) { int u = q.front(); q.pop(); for(int i = first[u];i != -1;i = nxt[i]) { int v = vv[i],c = cost[i]; if(c > 0 && dis[v] < 0) { dis[v] = dis[u] + 1; q.push(v); } } } if(dis[t] > 0) return 1; else return 0;}int dfs(int s,int t,int low){ int a = 0; if(s == t) return low; for(int i = first[s];i != -1;i = nxt[i]) { int v = vv[i],c = cost[i];///cost是容量 if(c > 0 && dis[v] == dis[s] + 1 && (a=dfs(v,t,min(low,c)))) { if(i&1) { cost[i] -= a; cost[i+1] += a; return a; } else { cost[i] -= a; cost[i-1] += a; return a; } } } return 0;}void Dinic(int s,int t,int n){ int a; int Max = 0; while(bfs(s,t)) { while(a = dfs(s,t,inf)) Max += a; } printf("%d\n",Max);}int main(){ int n,m; while(scanf("%d%d",&n,&m)==2) { e = 1; memset(first,-1,sizeof(first)); for(int i=1;i<=n;i++) { int a,b,w; scanf("%d%d%d",&a,&b,&w); add(a,b,w); } Dinic(1,m,m); } return 0;}
- HDU1532--Drainage Ditches
- hdu1532 Drainage Ditches
- hdu1532 Drainage Ditches (Dinic)
- hdu1532 Drainage Ditches
- HDU1532 Drainage Ditches
- hdu1532 Drainage Ditches
- HDU1532----Drainage Ditches
- Drainage Ditches [HDU1532]
- hdu1532-Drainage Ditches
- HDU1532 Drainage Ditches (maxflow_Dinic)
- POJ1273&&HDU1532-Drainage Ditches
- hdu1532 Drainage Ditches
- HDU1532-Drainage Ditches
- HDU1532 Drainage Ditches 最大流
- HDU1532 Drainage Ditches Dinic算法
- HDU1532 Drainage Ditches 【最大流】
- HDU1532 - Drainage Ditches(网络流)
- HDU1532 Drainage Ditches(网络流模板)
- 大话设计模式 享元模式
- cmd命令行进入相关目录
- HDU-1028Ignatius and the Princess III(母函数)
- 大三下学期,
- CTreeCtrl 自绘
- HDU1532--Drainage Ditches
- 设计一个类,该类不能被继承,且只能实例化3次
- 今天的收获 2013.5.3
- 大三时MySQL课程设计《MySQL集群的研究与实现》
- AndBug调试Android Dalvik Bytecode
- 索引总结
- 算法:静态查找表(Static Search Table)(顺序查找、二分查找、插值查找、斐波纳契查找)
- 13年五月至九月计划
- STM32用IO口控制步进电机的简单程序