POJ 1273:Drainage Ditches 网络流模板题
2015-09-08 20:04
603 查看
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
给了M个池塘,N个水渠,以及每个水渠连接的两个点和水渠的容量。(编号1为源点,编号M为汇点)。求整个网络中最大能流的水的流量。
网络流模板题。
代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 63339 | Accepted: 24434 |
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
给了M个池塘,N个水渠,以及每个水渠连接的两个点和水渠的容量。(编号1为源点,编号M为汇点)。求整个网络中最大能流的水的流量。
网络流模板题。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <queue> #include <cstring> #pragma warning(disable:4996) using namespace std; const int sum = 201; const int INF = 99999999; int cap[sum][sum],flow[sum][sum],a[sum],p[sum]; int N,M; void Edmonds_Karp() { int u,t,start=1,result=0; queue <int> s; while(s.size())s.pop(); while(1) { memset(a,0,sizeof(a)); memset(p,0,sizeof(p)); a[1]=INF; s.push(1); while(s.size()) { u=s.front(); s.pop(); for(t=1;t<=M;t++) { if(!a[t]&&flow[u][t]<cap[u][t]) { s.push(t); p[t]=u; a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量 } } } if(a[M]==0) break; result += a[M]; for(u=M;u!=1;u=p[u]) { flow[p[u]][u] += a[M]; flow[u][p[u]] -= a[M]; } } cout<<result<<endl; } int main() { int i,u,v,value; while(scanf("%d%d",&N,&M)==2) { memset(cap,0,sizeof(cap)); memset(flow,0,sizeof(flow)); for(i=1;i<=N;i++) { scanf("%d%d%d",&u,&v,&value); cap[u][v] += value; } Edmonds_Karp(); } return 0; }
相关文章推荐
- POJ 1149:PIGS 网络流经典题
- POJ 1149:PIGS 网络流经典题
- HTTP - 条件请求
- iOS 异步请求网络数据,传值问题
- 传输层(2)-TCP连接的建立和终止、TIME_WAIT状态
- 27-监管网络状态信息netstat
- 网络通信之网页源码查看 并显示屏幕上
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- Android第六讲——数据存储(五种)SharedPreferences、内部存储(缓存cache)、外部存储(Sdcrad)、SQLite数据库、网络存储
- HTTP学习
- 使用IOCP需要注意的一些问题~~(不断补充)
- iOS基础知识:Objective-C 之 网络请求
- TCP是如何保证包的顺序传输?
- nginx_tcp模块集成到openresty
- linux基础操作--网络配置
- 解读Android之HttpURLConnection
- [PHP自动化-进阶]003.CURL处理Https请求访问
- Android OkHttp完全解析
- tcpdump查看某个端口数据
- 【攻克Android (41)】HttpURLConnection