POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch
Time Limit 1000 ms
Memory Limit 65536 kb
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. Note however,
that there can be more than one ditch between two intersections.
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
Input file contains multiple test cases.
In a test case:
Line 1: 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.
Line 2..N+1: Each of 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,One line with 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
source
USACO 4.2
题意:就是给出各个边的最大流量,和起点终点,求最大流。
Edmonds-Karp 增广路算法
Code:
//Edmondes-Karp
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x7fffffff
using namespace std;
queue<int> q;
const int maxn = 200;
int n, m, ans;
int next[maxn+10], p[maxn+10], f[maxn+10][maxn+10], cap[maxn+10][maxn+10];
int Edmondes_Karp(int s, int t) {
int ans = 0, v, u;
queue<int> q;
memset(f,0,sizeof(f));
while(true) {
memset(p,0,sizeof(p));
p[s] = INF;
q.push(s);
while(!q.empty()) { //BFS找增广路
int u = q.front();
q.pop();
for(v=1; v<=m; v++)
if(!p[v]&&cap[u][v]>f[u][v]) { //找到新节点v
next[v] = u; //记录v的父亲,并加入FIFO队列
q.push(v);
p[v] = p[u] < cap[u][v]-f[u][v]?p[u] : cap[u][v] - f[u][v];
//s-v路径上的最小残量
}
}
if(!p[t]) break; //找不到增广路,则当前流已经是最大流
for(u=t; u!=s; u= next[u]) { //从汇点往回走
f[next[u]][u] +=p[t];//更新正向流量
f[u][next[u]] -=p[t];//更新反向流量
}
ans += p[t]; //更新从s流出的总流量
}
return ans;
}
int main() {
int i, k, k1, k2, k3;
while(~scanf("%d%d",&n,&m)) {
memset(cap,0,sizeof(cap));
for(i=1; i<=n; i++) {
scanf("%d%d%d",&k1,&k2,&k3);
cap[k1][k2] +=k3;
}
printf("%d\n",Edmondes_Karp(1,m) );
}
return 0;
}
POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)的更多相关文章
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- hdu 1532 Drainage Ditches (最大流)
最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- HDU 1532 Drainage Ditches(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
随机推荐
- hdu 4642 Fliping game(博弈)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4642 题意:给定一个棋盘,0表示向下,1表示向上,选一个x,y, 然后翻转从x,y 到n,m.的所有硬币, ...
- POI使用cell.getCellStyle()设置指定单元格颜色,但是其它没有指定的单元格也会变色
HSSFCell cell = row.createCell((short)i); cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT ...
- ora-28001:口令失效
Oracle11G创建用户时缺省密码过期限制是180天(即6个月), 如果超过180天用户密码未做修改则该用户无法登录. Oracle公司是为了数据库的安全性默认在11G中引入了这个默认功能,但是这个 ...
- 【转】禁止seekbar的拖动事件
原文网址:http://blog.csdn.net/ansionnal/article/details/8229801 当然是可以的! 其实是 onTouchEvent 事件时,不让他传递事件就行了! ...
- OpenGL学习之路(五)
1 引子 不知不觉我们已经进入到读书笔记(五)了,我们先对前四次读书笔记做一个总结.前四次读书笔记主要是学习了如何使用OpenGL来绘制几何图形(包括二维几何体和三维几何体),并学习了平移.旋转.缩放 ...
- (四)学习JavaScript之className属性
参考:http://www.w3school.com.cn/jsref/prop_classname.asp HTML DOM Anchor 对象 定义和用法 className 属性设置或返回元素的 ...
- java中的快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- 15、NDK开发初步
一.什么是NDK? NDK是Android让你潜入原生组件(C/C++开发)的一套开发套件 Android应用程序是运行在Dalvik虚拟机中的 ,NDK允许你通过原生代码实现部分的应用程序模块 . ...
- [selenium webdriver Java]检查元素状态
许多测试失败是因为点击一个元素失败或者在一个不可见的字段中输入文字,或者是在不可输入的文本中输入文字. 我们可以在具体操作之前,检查一下元素的状态.WebElement类提供了这样的方法. 方法 目的 ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...