poj Drainage Ditches 1273 (最大流入门)
2015-11-02 22:21
309 查看
Drainage Ditches
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.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64026 | Accepted: 24713 |
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.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
题意:(最大流模板)
有n个城市,n条路,出发点在1,终点在m,让求最大流。
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #define INF 0x3f3f3f3f #define N 220 using namespace std; int p ; bool vis ; int map ; int s,e; int n,m; bool bfs() { queue<int>q; memset(p,0,sizeof(p)); memset(vis,false,sizeof(vis)); vis[s]=1; q.push(s); while(!q.empty()) { int ss=q.front(); q.pop(); if(ss==e) return true;//如果找到一条从1到n的路。 for(int i=2;i<=n;i++) { if(!vis[i]&&map[ss][i])//如果可到达,但还没访问,遍历所有与i相连的城市 { q.push(i); p[i]=ss;//p[]数组里存放父节点 vis[i]=1; } } } return false;//找不到 } int maxflow() { int ans=0;//记录最大流 while(1) { if(!bfs())//如果没有从1到n的路 return ans;//直接输出流量 int mm=INF; for(int i=e;i!=s;i=p[i])//从终点往起点找(从最小子孙往祖宗找) mm=min(mm,map[p[i]][i]);//找到关系网中的最小的残留量。 for(int i=e;i!=s;i=p[i])//逐个更新 { map[p[i]][i]-=mm;//正着的(父到子) map[i][p[i]]+=mm;//反着的(子到父) } ans+=mm;//更新最大流 } } int main() { int u,v,w; while(scanf("%d%d",&m,&n)!=EOF) { s=1;e=n; memset(map,0,sizeof(map)); while(m--) { scanf("%d%d%d",&u,&v,&w); map[u][v]+=w;//每条边赋初值 } printf("%d\n",maxflow()); } return 0; }
相关文章推荐
- Delete Duplicate Emails---easy
- ADB server didn't ACK fail to start daemon解决方案
- Daily Scrumming* 2015.11.2(Day 14)
- toj 3072 Train Order
- 线程:sleep()、wait()、yield()和join()方法
- poj 1363Rails
- hdu1789 Doing Homework again (贪心+优先队列)
- AIDL进阶
- adb server is out of date. killing... ADB server didn't ACK * failed to star
- AI 行为树框架
- 关于exchange使用foxmail下的pop3无法发送邮件exchange模式正常
- LightOJ 1021 - Painful Bases (状压dp)
- Quartz 任务调度(二)
- ZOJ 2967-C - Colorful Rainbows
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- Quartz 任务调度(一)
- AIDL初探(本地、进程)
- codeforces 242A Heads or Tails
- [11]Container With Most Water
- Kafka设计解析:Kafka High Availability (下)