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实现,把很多以前运行于单 ...
随机推荐
- Request、Servlet及其子接口
最近看tomcat源码,这类接口多的有点眩,整理出来看一下.(基于tomcat4) javax.servlet.ServletRequset接口,和org.apache.catalina.Reques ...
- Troubleshooting JDK
收集整理下JDK自带的关于 Troubleshooting 的文档 Java 2 Platform, Standard Edition 5.0 Troubleshooting and Diagnost ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- oracle 分页(rownum的理解) 以及 树节点的查询
1:什么是rownum, rownum的生成, rownum相关的符号操作 Rownum是oracle生成结果集时得到的一个伪列, 按照读出行的顺序, 第一条rownum=1, 第二条=2. 对于 O ...
- HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...
- demo07
city_data.xml <?xml version="1.0" encoding="utf-8"?> <resources> < ...
- MSSQL 判断实例中是否存在某种表
执行语句 SELECT 'SELECT * FROM '+Name+'..SysObjects Where XType=''U'' and name=''tab_scartrim'' ORDER BY ...
- OpenCV 简介
自版本OpenCV2.2开始,OpenCV库便被划分为多个模块.这些模块编译成库文件后,位于lib文件夹中. opencv_core模块,包含核心功能,尤其是底层数据结构和算法函数. opencv_i ...
- Openstack的HA解决方案【替换原有的dashboard】
0. 进入到/etc/haproxy/conf.d/目录下 mv 015-horizon.cfg 150-timaiaas.cfg 将原有的dashboard的ha配置文件做为自己的配置文件. 1. ...