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) 收藏的更多相关文章

  1. 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) ...

  2. Javascript图片预加载详解 分类: JavaScript HTML+CSS 2015-05-29 11:01 768人阅读 评论(0) 收藏

    预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...

  3. UI基础:UIView(window,frame,UIColor,CGPoint,alpha,CGRect等) 分类: iOS学习-UI 2015-06-30 20:01 119人阅读 评论(0) 收藏

    UIView 视图类,视图都是UIView或者UIView子类 UIWindow 窗口类,用于展示视图,视图一定要添加window才能显示 注意:一般来说,一个应用只有一个window 创建一个UIW ...

  4. OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏

    1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...

  5. 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  6. javascript中0级DOM和2级DOM事件模型浅析 分类: C1_HTML/JS/JQUERY 2014-08-06 15:22 253人阅读 评论(0) 收藏

    Javascript程序使用的是事件驱动的设计模式,为一个元素添加事件监听函数,当这个元素的相应事件被触发那么其添加的事件监听函数就被调用: <input type="button&q ...

  7. 将HTML格式的String转化为HTMLElement 分类: C1_HTML/JS/JQUERY 2014-08-05 12:01 1217人阅读 评论(0) 收藏

    代码如下: <meta charset="UTF-8"> <title>Insert title here</title> </head& ...

  8. 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 ...

  9. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

随机推荐

  1. Redis:安装

    此篇文章并不介绍linux下安装方法.围绕windows安装而介绍 两种安装方式: 1)下载压缩包,解压后(运行起来有个窗口,关闭掉就不在运行),没有windows服务被注册:可以只用命令在cmd将其 ...

  2. Angular.js 学习笔记

    AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序. <!-- ng-app 指令定义一个 AngularJS 应用程序. ng-mod ...

  3. [转]Java中的多线程你只要看这一篇就够了

    如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个话其 ...

  4. PHP-字符串过长不用担心

    字符串过长可以用定界符: >>>GOD ................巨长务必的字符串 GOD;

  5. C++之路进阶——codevs2404(糖果)

    2404 糖果 2011年省队选拔赛四川  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 幼儿园里有N个小朋友,l ...

  6. paper 34 :常见函数的举例(更新ing)2

    在研究opencv,不是很难,但是需要换种思维来认知这个C/C++为编程函数的开源代码库,OK,我现在还是总结一些常用MATLAB的函数,随时更新,下一阶段就是opencv方面的认知了! 1.std ...

  7. 深度学习 vs 机器学习 vs 模式识别

    http://www.csdn.net/article/2015-03-24/2824301 [编者按]本文来自CMU的博士,MIT的博士后,vision.ai的联合创始人Tomasz Malisie ...

  8. python pdb

    pdb 以参数-m pdb启动后,pdb定位到下一步要执行的代码-> s = '0'.输入命令l来查看代码: 输入命令n可以单步执行代码: 任何时候都可以输入命令p 变量名来查看变量: (Pdb ...

  9. 超链接点击后不显示hover

    超链接访问过后 hover 样式就不出现了,被点击访问过的超链接样式不在具有 hover 和 active 了 解决方法:改变CSS属性的排列顺序 L-V-H-A a:link {} a:visite ...

  10. 非 动态规划---LIS

    题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度.(见动态规划---LIS) /* 题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度 ...