网络流 - Drainage Ditches - HDU - 1532
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.
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
思路:如果从a到b能够进行广搜,那么广搜后将路径标记,为了反悔还需要e[dis[i]][i]-=mx;e[i][dis[i]]+=mx;
。广搜中如果能够进行需要dis[i]=x;
来标记与x相连的为i。
完整代码为
#include<bits/stdc++.h>
using namespace std;
const int N=310;
int n,dis[N],bk[N],e[N][N];
int bfs(int a,int b)
{queue<int>q;memset(bk,0,sizeof(bk));memset(dis,-1,sizeof(dis));bk[a]=1,dis[a]=a;q.push(a);while(!q.empty()){int x=q.front();q.pop();for(int i=1; i<=n; i++){if(!bk[i]&&e[x][i]){bk[i]=1;dis[i]=x;q.push(i);if(i==b) return 1;}}}return 0;
}
int ek(int a,int b)
{int i,mx,ans=0;while(bfs(a,b)){mx=INT_MAX;for(i=b; i!=a; i=dis[i])mx=min(mx,e[dis[i]][i]);for(i=b; i!=a; i=dis[i]){e[dis[i]][i]-=mx;e[i][dis[i]]+=mx;}ans+=mx;}return ans;
}
int main()
{int a,b,c,m;while(cin>>m>>n){memset(e,0,sizeof(e));memset(bk,0,sizeof(bk));memset(dis,-1,sizeof(dis));while(m--){cin>>a>>b>>c;e[a][b]+=c;}cout<<ek(1,n)<<endl;}
}
网络流 - Drainage Ditches - HDU - 1532相关推荐
- 网络流--最大流--POJ 1273 Drainage Ditches
链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...
- Drainage Ditches POJ1273
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 93263 Accepted: 36174 试题链接 文章目录 Descripti ...
- USACO Section 4.2 Drainage Ditches(最大流)
最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...
- [POJ 1273]Drainage Ditches
[问题描述] Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch ...
- poj1273 Drainage Ditches
蒟蒻的blog POJ 1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Memory Limit: 10000K Total ...
- HDU 1532 Drainage Ditches(poj1273)【E-K 最大流】
第一道最大流题. 水题,裸题. Sample Input 5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10 Sample Output 50 输入m n, m是边数,n是点 ...
- usaco Drainage Ditches(网络流dinic模板)
模板题 /* ID:jinbo wu TASK:ditch LANG:C++ */#include<bits/stdc++.h> using namespace std; #define ...
- 【网络流】解题报告:luogu P2740 [USACO4.2]草地排水Drainage Ditches(Edmonds-Karp增广路,最大流模板)
题目链接:草地排水 若一条从源点到汇点的路径上各条边的剩余容量都大于0,则称这条路径为一条增广路. Edmonds-Karp增广路的策略就是不断用bfs寻找增广路,直至网络中不在存在增广路为止. 在每 ...
- Drainage Ditches - poj 1273(网络流模板)
题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...
最新文章
- 网页布局(div布局)
- javascript深入理解js闭包[转]
- Linux TCPIP内核参数优化
- Linux(UOS) Qt不能播放音频的问题
- 编写声卡驱动(框架)
- Linux学习笔记-消息队列概念
- 799元!乐视智能门锁新品Le1S发布
- mybatis ------ mybatis和spring整合(十一)
- PyCharm报错ModuleNotFoundError: No module named requests
- android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...
- 活动 | 腾讯云证通 给你送长鹅~
- 传奇物品在地上显示,红字,自动拾取文件
- 五笔:25 个一级简码(口诀)
- openbmc开发30:webui开发—基础
- Win10系统怎么还原?Win10如何回到以前的系统
- 正则表达式随记(3)常用的正则表达式
- 网站更换国外服务器,备案被注销替换外国服务器?这样合理吗?
- 按键精灵脚本笔记 鼠标按直线轨迹移动
- 五类千万不要吃的动物部位
- CDO安装指南(centos7)