Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 62016 Accepted: 23808
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
题意:John为了不让Bessie最喜欢的三叶草被雨水淹没,在农场修建了一套排水系统,将雨水排到小河里,在每个排水沟的起点安置了调节阀门,
计算排水系统的最大流水速度
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#define WW freopen("a1.txt","w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 220;
int Map[MAX][MAX];
int Dis[MAX];
int n,m;
int BFS()//通过广度优先搜索对残留网络进行分层,建立分层网络
{
int ans;
queue<int>Q;
memset(Dis,-1,sizeof(Dis));
Dis[1]=0;
Q.push(1);
while(!Q.empty())
{
ans=Q.front();
Q.pop();
for(int i=1;i<=n;i++)
{
if(Dis[i]<0&&Map[ans][i]>0)
{
Dis[i]=Dis[ans]+1;
Q.push(i);
}
}
}
if(Dis[n]>0)//判断是不是能到达汇点,如果能到达汇点,则存在增广路,则要进行Dinic算法进行增广,否则就已經求出最大流
{
return 1;
}
return 0;
}
int Dinic(int x,int Min)//对分层网络进行增广,通过DFS的形式进行多次的增广
{
if(x==n)
{
return Min;
}
int ans;
for(int i=1;i<=n;i++)
{
if(Map[x][i]>0&&Dis[i]==Dis[x]+1&&(ans=Dinic(i,min(Min,Map[x][i]))))//对于x它要访问的下一个点的层次一定比它大一,一开始写成(Dis[x]==Dis[i]+1)
{ //结果死循环。。。。。。。。。。。
Map[x][i]-=ans;
Map[i][x]+=ans;
return ans;
}
}
return 0;//如果到达不了汇点,则整个网络已经增广完毕,需要重新的进行分层
}
int main()
{
int u,v,w;
int sum;//记录最大流
while(~scanf("%d %d",&m,&n))
{
memset(Map,0,sizeof(Map));
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
Map[u][v]+=w;
}
sum=0;
int ans;
while(BFS())
{
while(ans=Dinic(1,INF),ans)
{
sum+=ans;
}
}
printf("%d\n",sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏的更多相关文章
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Javascript图片预加载详解 分类: JavaScript HTML+CSS 2015-05-29 11:01 768人阅读 评论(0) 收藏
预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...
- UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏
UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- javascript中0级DOM和2级DOM事件模型浅析 分类: C1_HTML/JS/JQUERY 2014-08-06 15:22 253人阅读 评论(0) 收藏
Javascript程序使用的是事件驱动的设计模式,为一个元素添加事件监听函数,当这个元素的相应事件被触发那么其添加的事件监听函数就被调用: <input type="button&q ...
- 将HTML格式的String转化为HTMLElement 分类: C1_HTML/JS/JQUERY 2014-08-05 12:01 1217人阅读 评论(0) 收藏
代码如下: <meta charset="UTF-8"> <title>Insert title here</title> </head& ...
- XHTML 结构化:使用 XHTML 重构网站 分类: C1_HTML/JS/JQUERY 2014-07-31 15:58 249人阅读 评论(0) 收藏
http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...
- Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏
Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...
随机推荐
- 【你吐吧c#每日学习】10.30 C#Nullable Types
分两种类型,value type and reference type. By default, value type owns a default value. For integer, the d ...
- Java: xml转换
java对于xml的转换有很多种,比较有名的有:DOM, DOM4J, JDOM, SAX.这里要介绍的是javax.xml包的对xml文件的转换.相比于前面几种是最简单的. 直接上代码: Stude ...
- extjs4.0下的日期控件的星期显示为y的解决办法
没有修改的时候的问题: 今天第一次写博客,就记录一下以前extjs4.2下运用日期组件的星期显示问题,当时找了n久,可能是extjs4.2才出来没多久,没有多少人发现这个问题或者说很少有人将Extjs ...
- eclipse中修改maven仓储
1.找到maven的setting文件,修改setting文件: 2.打开eclipce,window->Preference->maven->user Setting
- java中的Robot
项目中最近遇到了一个棘手问题,无法用WebDriver去操作win弹出窗口,经过多番查找,发现了Robot这个奇葩东东,Robot可以模拟鼠标和键盘操作, robot可以实现本地系统输入空包括win弹 ...
- ligerui_ligerTree_002_利用JavaScript代码配置ligerTree节点
利用JavaScript代码配置ligerTree节点: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图: <%@ p ...
- 解决ScrollView与ListView事件冲突
1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...
- 如何使用highmaps制作中国地图
如何使用highmaps制作中国地图 文章目录 Highmaps 所需文件 地图初始化代码 highmaps 渲染讲解 highmaps 中国各城市坐标的json文件 highmaps 线上DEMO ...
- 夺命雷公狗ThinkPHP项目之----企业网站17之网站配置页的添加
为了网站可以智能一点,所以我们开始来写一个网站配置的功能.. 所以我来写他的数据表: 先来完成他的添加功能,页面效果如下所示: lists.html代码如下所示: <!doctype html& ...
- 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)
我们刚才只是完成了添加的一部分,但是我们的上级分类也不能永远都是只有一个死的嘛,所以我们需要对她进行修改: 我们先将add方法里面的数据查出来再说: 然后在模板页进行遍历: 展示效果如下所示: 虽然是 ...