POJ
来源:互联网 发布:bonjour是什么软件 编辑:程序博客网 时间:2024/06/17 08:52
题目链接
Drainage Ditches
POJ - 1273Every 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.
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.
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
5 41 2 401 4 202 4 202 3 303 4 10
50
思路:这是个最大流的模板题,很容易理解。
#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;#define MAX 0x3f3f3f3f#define maxn 1010int map[maxn][maxn]; //原始最大的网络图int flow[maxn][maxn]; //能到终点的流量图int dis[maxn]; //标记父亲节点int vis[maxn]; //记录流到终点的流量int m,n;
int Ford_fulkerson(int a,int k){ queue<int>Q; memset(flow,0,sizeof(flow)); int sum=0; while(1) { memset(vis,0,sizeof(vis)); vis[a]=MAX; Q.push(a); while(!Q.empty()) { int p=Q.front(); Q.pop(); for(int i=1; i<=k; i++) if(!vis[i]&&map[p][i]>flow[p][i]) //vis[i]判断不要出现循环 { dis[i]=p; //记录父亲节点 Q.push(i); //入队 vis[i]=min(vis[p],map[p][i]-flow[p][i]);//寻找最小的流量 } } sum+=vis[k]; //寻找到一个增广路线,进行累加, if(vis[k]==0) //无增广路线, return sum; for(int i=k;i!=a;i=dis[i]) //找到一条增广路线,要把flow进行更新。 { flow[i][dis[i]]-=vis[k]; //从终点开始,dis[i]是i的父亲节点,所以减少。 flow[dis[i]][i]+=vis[k]; //从终点开始,dis[i]是i的父亲节点,所以相加。 } }}int main(){ while(~scanf("%d %d",&m,&n)) { int a,b,c; memset(map,0,sizeof(map)); for(int i=1; i<=m; i++) { scanf("%d %d %d",&a,&b,&c); map[a][b]+=c; } printf("%d\n",Ford_fulkerson(1,n)); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Calendar类的常用方法
- 飞行汽车,带飞你的时代将近
- Spring Data Elasticsearch
- leetcode 310. Minimum Height Trees 最小高度树 + DFS深度优先遍历 + 拓扑排序
- 人生就是一个高斯分布或者是一个冲击函数
- POJ
- 2017年Android面试复习资料整理
- 定制化删除ES索引数据
- 苹果开源iOS和macOS内核源代码 | 十一献礼
- Java java.lang.Math数学包
- [算法分析与设计] leetcode 每周一题: 335. Self Crossing
- 2017.10.2 探寻本质
- String 常量池的使用
- HDU