Drainage Ditches
来源:互联网 发布:不干胶打印软件 编辑:程序博客网 时间:2024/06/01 09:44
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 <cstdio>#include <cstring>#include <queue>#define mem(a) memset(a,0,sizeof(a));using namespace std;const int INF = 0x3fffffff;int surplus[300][300];int pre[300];int flow[300];queue<int> q;int bfs(int s,int d){while(!q.empty()){q.pop();}memset(pre,-1,sizeof(pre));q.push(s);while(!q.empty()){int t = q.front();q.pop();for(int i = 1;i <= d;i++){if(surplus[t][i] > 0 && pre[i] == -1){flow[i] = min(surplus[t][i],flow[t]);pre[i] = t;if(i == d){return flow[d];}q.push(i);}}}return -1;}int maxFlow(int s,int d){int res= 0;int plus = 0;pre[s] = s;flow[s] = INF;while((plus = bfs(s,d)) != -1){res += plus;for(int i = d;i != s;i = pre[i]){surplus[pre[i]][i] -= plus;surplus[i][pre[i]] += plus;}}return res;}int main(){int n,m;while(scanf("%d%d",&n,&m) != EOF){mem(surplus);for(int i = 0;i < n;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);surplus[u][v] += c;}printf("%d\n",maxFlow(1,m));}return 0;}
0 0
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- POJ1273 Drainage Ditches
- PKU 1273 Drainage Ditches
- Drainage Ditches 最大流
- mysql中字符串的连接
- A* 算法详解
- 理解Quartz原理
- WCF异常信息:由于内部错误,服务器无法处理该请求...
- 大公司和小公司的抢人战,孰胜孰负?
- Drainage Ditches
- Remove Duplicates from Sorted List
- 【bzoj3163】 HEOI2013Eden的新背包问题 多重背包
- Java 使用线程池递归压缩一个文件夹下的所有子文件
- JavaSE----面向对象(形式参数和返回值研究、包、修饰符、内部类)
- Java窗体加载时组件不显示的解决方法
- 微信分享失败之checkArgs fail, thumbData is invalid
- Java核心技术第9版中文扫描版
- Array()