POJ1273 HDU1532 Drainage Ditches
2015-11-25 14:00
405 查看
题目链接:http://poj.org/problem?id=1273
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.
裸最大流Dinic多路增广模版。CodeVS上AC,POJ上没看到The input includes several cases. WA了n次呵呵……
View Code
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.
裸最大流Dinic多路增广模版。CodeVS上AC,POJ上没看到The input includes several cases. WA了n次呵呵……
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #define rep(i,l,r) for(int i=l; i<=r; i++) #define clr(x,y) memset(x,y,sizeof(x)) #define travel(x) for(int i=last[x]; i!=-1; i=edge[i].pre) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 210; struct Edge{ int pre,to,cost; }edge[maxn<<1]; int m,n,x,y,z,now,tot,ans,last[maxn],d[maxn],cur[maxn]; queue <int> q; inline void addedge(int x,int y,int z){ edge[++tot].pre = last[x]; edge[tot].to = y; edge[tot].cost = z; last[x] = tot; edge[++tot].pre = last[y]; edge[tot].to = x; edge[tot].cost = 0; last[y] = tot; } bool bfs(){ while (!q.empty()) q.pop(); clr(d,-1); d[1] = 0; q.push(1); while (!q.empty()){ now = q.front(); q.pop(); travel(now){ if (d[edge[i].to] == -1 && edge[i].cost > 0){ d[edge[i].to] = d[now] + 1; q.push(edge[i].to); } } } return d != -1; } int dfs(int x,int in){ if (x == n) return in; int w = 0; for(int i=cur[x]; i != -1 && w < in; i=edge[i].pre){ if (d[edge[i].to] == d[x] + 1 && edge[i].cost > 0){ int delta = dfs(edge[i].to,min(in-w,edge[i].cost)); edge[i].cost -= delta; edge[i^1].cost += delta; if (edge[i].cost) cur[x] = i; w += delta; } } if (w < in) d[x] = -1; return w; } int main(){ while (scanf("%d%d",&m,&n) == 2){ tot = -1; ans = 0; clr(last,-1); rep(i,1,m){ scanf("%d%d%d",&x,&y,&z); addedge(x,y,z); } while (bfs()){ rep(i,1,n) cur[i] = last[i]; int nowans = dfs(1,0x7fffffff); ans += nowans; } printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- sizeof与offsetof有关的结构体详解 http://blog.csdn.net/callinglove/article/details/46534883
- 二叉树学习之二叉查找树 http://blog.csdn.net/callinglove/article/details/40707449
- Failed to configure driver: unknown error. Failed to open driver
- 线程之wait与sleep的区别
- nankai100_4
- [LeetCode256]Paint House
- nankai100_3
- 指针属性直接赋值 最好先retain 否则内存释放导致crash
- 使用mailx借助外网邮箱地址发送邮件
- AIDL原理解析
- 百度地图api出现 leaked ServiceConnection com.baidu.location.LocationClient错误
- rand()和srand((unsigned)time(NULL))产生的随机数有什么不同http://blog.csdn.net/jinjingwen/article/details/6205537
- C++中的srand(time(null))利用时间设置随机种子产生随机数 http://blog.csdn.net/youoran/article/details/10950671
- nankai100_2
- FlashRAID 发布 与PBlaze4共筑企业固态存储解决方案
- nankai100_1
- HaiHongOJ 1003 God Wang
- Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
- RAID-4与模2和
- 学习笔记7:wait(),notify()与notifyAll()及消费者生产者小案例