2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
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
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
USACO 93
一道最大流的板子题,感觉也没什么好说的,建图也比较easy" role="presentation" style="position: relative;">easyeasy,直接套个dinic" role="presentation" style="position: relative;">dinicdinic就行了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define oo 0x3f3f3f3f
#define N 10005
using namespace std;
struct Node{
int v,next,c;
}e[N<<1];
int tot=1,n,m,d[N],first[N];
inline void add(int u,int v,int c){
e[++tot].v=v;
e[tot].next=first[u];
e[tot].c=c;
first[u]=tot;
}
inline long long read(){
long long ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch)){
ans=(ans<<3)+(ans<<1)+ch-'0';
ch=getchar();
}
return ans;
}
inline int max(int a,int b){return a>b?a:b;}
inline bool bfs(){
queue<int>q;
q.push(1);
memset(d,-1,sizeof(d));
d[1]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=first[x];i!=-1;i=e[i].next){
if(d[e[i].v]==-1&&e[i].c){
d[e[i].v]=d[x]+1;
if(e[i].v==m)return true;
q.push(e[i].v);
}
}
}
return false;
}
inline int dfs(int p,int f){
if(p==m)return f;
int cnt=f;
for(int i=first[p];i!=-1;i=e[i].next){
int v=e[i].v;
if(d[v]==d[p]+1&&e[i].c>0&&cnt){
int maxn=dfs(v,min(cnt,e[i].c));
if(maxn==0)d[v]=-1;
cnt-=maxn;
e[i].c-=maxn;
e[i^1].c+=maxn;
}
}
return f-cnt;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
tot=1;
memset(first,-1,sizeof(first));
for(int i=1;i<=n;++i){
int u=read(),v=read(),w=read();
add(u,v,w);
add(v,u,0);
}
int ans=0;
while(bfs())ans+=dfs(1,oo);
printf("%d\n",ans);
}
return 0;
}
2018.07.06 POJ1273 Drainage Ditches(最大流)的更多相关文章
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- poj1273 Drainage Ditches (最大流板子
网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...
- [poj1273]Drainage Ditches(最大流)
解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- poj1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68414 Accepted: 2648 ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
随机推荐
- JS实现拖动效果
有个问题就是该模块要使用定位,因为有left,top属性使用,绝对定位和相对定位都行,当然你也可使用margin-left,和margin-top这2个属性,替换left,top也是可以得 这样就不用 ...
- UI5-文档-4.34-Custom Controls
在这一步中,我们将使用自定义控件扩展SAPUI5的功能.我们希望对详细页面上显示的产品进行评级,因此我们使用SAPUI5扩展机制创建了多个标准控件的组合,并添加了一些粘合代码以使它们能够很好地一起工作 ...
- __builtin__与__builtins__的区别与关系
在学习Python时,很多人会问到__builtin__.__builtins__和builtins之间有什么关系.百度或Google一下,有很 多答案,但是这些答案要么不准确,要么只说了一点点,并不 ...
- c#栈的习题2
—.单项选择题1.栈和队列具有相同的( ). A.抽象数据类型 B.逻辑结构 C.存储结构 D.运算2.栈是(). A.顺序存储的线性结构 B.链式存储的非线性结 ...
- pyplot图像组件
pyplot图像组件 ax子对象的组件内容 Title 图表标题 plt.title() Axis 坐标范围,x轴,y轴 plt.axis() label 坐标轴标注 plt.xlabel() plt ...
- oracle系统视图字段说明
oracle系统表v$session.v$sql表的列字段说明 在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, a ...
- 用Python写单向链表和双向链表
链表是一种数据结构,链表在循环遍历的时候效率不高,但是在插入和删除时优势比较大. 链表由一个个节点组成. 单向链表的节点分为两个部分:存储的对象和对下一个节点的引用.注意是指向下一个节点. 而双向链表 ...
- Hook钩子编程
钩子(Hook),是Windows消息处理机制的一个平台,钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统,以监视指定窗口的某种消息.每当特定的消息发出,在没有到达目的窗口前,钩子程序就先 ...
- Unity3d dll 热更新 基础框架
APK包装到用户手机上后,代码如何更新,总不能全用LUA吧?特别是代码非常多的战斗手游 昨晚上有了dll 热更新的想法,今天做了一天的实验,也遇到些坑,这里总结下 工作环境: U3D5.3.2 + v ...
- Distributing Ballot Boxes
Distributing Ballot Boxes http://acm.hdu.edu.cn/showproblem.php?pid=4190 Time Limit: 20000/10000 MS ...