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相关推荐

  1. 网络流--最大流--POJ 1273 Drainage Ditches

    链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...

  2. Drainage Ditches POJ1273

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 93263 Accepted: 36174 试题链接 文章目录 Descripti ...

  3. USACO Section 4.2 Drainage Ditches(最大流)

    最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...

  4. [POJ 1273]Drainage Ditches

    [问题描述] Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch ...

  5. poj1273 Drainage Ditches

    蒟蒻的blog POJ 1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Memory Limit: 10000K Total ...

  6. 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是点 ...

  7. usaco Drainage Ditches(网络流dinic模板)

    模板题 /* ID:jinbo wu TASK:ditch LANG:C++ */#include<bits/stdc++.h> using namespace std; #define ...

  8. 【网络流】解题报告:luogu P2740 [USACO4.2]草地排水Drainage Ditches(Edmonds-Karp增广路,最大流模板)

    题目链接:草地排水 若一条从源点到汇点的路径上各条边的剩余容量都大于0,则称这条路径为一条增广路. Edmonds-Karp增广路的策略就是不断用bfs寻找增广路,直至网络中不在存在增广路为止. 在每 ...

  9. Drainage Ditches - poj 1273(网络流模板)

    题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************* ...

最新文章

  1. 网页布局(div布局)
  2. javascript深入理解js闭包[转]
  3. Linux TCPIP内核参数优化
  4. Linux(UOS) Qt不能播放音频的问题
  5. 编写声卡驱动(框架)
  6. Linux学习笔记-消息队列概念
  7. 799元!乐视智能门锁新品Le1S发布
  8. mybatis ------ mybatis和spring整合(十一)
  9. PyCharm报错ModuleNotFoundError: No module named requests
  10. android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...
  11. 活动 | 腾讯云证通 给你送长鹅~
  12. 传奇物品在地上显示,红字,自动拾取文件
  13. 五笔:25 个一级简码(口诀)
  14. openbmc开发30:webui开发—基础
  15. Win10系统怎么还原?Win10如何回到以前的系统
  16. 正则表达式随记(3)常用的正则表达式
  17. 网站更换国外服务器,备案被注销替换外国服务器?这样合理吗?
  18. 按键精灵脚本笔记 鼠标按直线轨迹移动
  19. 五类千万不要吃的动物部位
  20. CDO安装指南(centos7)

热门文章

  1. 行业深度:区块链与金融、保险业那些不得不说的故事
  2. Android 混合开发之仿微信朋友圈
  3. 【zabbix】自定义监控、邮件报警、企业微信报警、钉钉报警(详细)
  4. mysql锁(全局锁、表锁、行锁、页锁、排他锁、共享锁)
  5. Kettle实验 (四)
  6. 2019美网男单决赛纳达尔第19大满贯冠
  7. ipad投屏到海信电视通用方法分享
  8. SAP 接口 inbound (SAP CALL JAVA ) 负载均衡说明
  9. DVWA 之 XSS(Reflected)反射型XSS
  10. c语言两位共阴数码管00,两位数码管动态显示C语言程序