Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15853    Accepted Submission(s): 7544

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
 
 
最大流裸题。Edmonds-Karp算法。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 200
#define LL long long
#define INF 2000000005 queue<int>q;
int flow[N][N];
int cap[N][N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(cap,,sizeof(cap));
for(int i=; i<n; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(cap[a][b]==)
{
cap[a][b]=c;
}
else
{
cap[a][b]+=c;
}
}
while(!q.empty())
q.pop();
int totf=;
int p[N];
memset(flow,,sizeof(flow));
for(;;)
{
int a[N];
memset(a,,sizeof(a));
a[]=INF;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=; v<=m; v++)
if(!a[v]&&cap[u][v]>flow[u][v])
{
p[v]=u;
q.push(v);
if(cap[u][v]-flow[u][v]>a[u])
a[v]=a[u];
else
a[v]=cap[u][v]-flow[u][v];
}
}
if(a[m]==)
break;
for(int u=m; u!=; u=p[u])
{
flow[p[u]][u]+=a[m];
flow[u][p[u]]-=a[m];
}
totf+=a[m];
//cout<<"here"<<endl;
}
printf("%d\n",totf);
}
return ;
}

HDU_1532_最大流的更多相关文章

  1. 使用C#处理基于比特流的数据

    使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...

  2. HTML 事件(三) 事件流与事件委托

    本篇主要介绍HTML DOM中的事件流和事件委托. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4 ...

  3. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  4. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  5. java 字节流与字符流的区别

    字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...

  6. BZOJ 3504: [Cqoi2014]危桥 [最大流]

    3504: [Cqoi2014]危桥 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1407  Solved: 703[Submit][Status] ...

  7. java I/O流

    输入流(读取数据的流) BufferedInputStream---继承--->FileInputStream--继承--->InputStream------> (1)字节流操作中 ...

  8. Ford-Fulkerson 最大流算法

    流网络(Flow Networks)指的是一个有向图 G = (V, E),其中每条边 (u, v) ∈ E 均有一非负容量 c(u, v) ≥ 0.如果 (u, v) ∉ E 则可以规定 c(u, ...

  9. .NET基础拾遗(3)字符串、集合和流

    Index: (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基础 ...

随机推荐

  1. 1154 能量项链 2006年NOIP全国联赛提高组 codevs

    1154 能量项链  2006年NOIP全国联赛提高组 codevs 题目描述 Description 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头 ...

  2. 17、Java并发性和多线程-避免死锁

    以下内容转自http://ifeve.com/deadlock-prevention/: 在有些情况下死锁是可以避免的.本文将展示三种用于避免死锁的技术: 加锁顺序 当多个线程需要相同的一些锁,但是按 ...

  3. JAVA包装类的缓存范围

    JAVA包装类的缓存范围 前两天面试遇到两个关于JAVA源码的问题,记录下来提醒自己. 1.写出下面的输出结果 System.out.println(Integer.valueOf("100 ...

  4. iOS常用的宏定义总结

    字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str le ...

  5. Js跨一级域名同步cookie

    1. 纯Js同步两个域名下的cookie document.cookie = "name=" + "value;" + "expires=" ...

  6. VB Socket编程 框架

    [转载]VB Socket编程 框架 (2014-07-15 20:06:28) 转载▼ 标签: 转载   原文地址:VB Socket编程 框架作者:安静的浪花 VB Socket编程(Winsoc ...

  7. Android 自己定义Activity基类

    我们在开发App的时候有时候碰到多个界面有一个共同点的时候.比方,都有同样的TitleBar.而且TitleBar能够设置显示的文字.TitleBar上的点击事件,假设给每个Activity都写一遍T ...

  8. 微软公有云Azure是惠及全人类的计算资源

    回归往事,1975年,微软以DOS创业.在随后的三十年中,微软给人类贡献了视窗操作系统Windows,至今,人们对桌面操作系统XP仍然不离不弃.可是,面对互联网的兴起.微软应该怎么办呢? 微软内部不乏 ...

  9. Linux下的ssh实验环境搭建与管理

    实验环境[size=10.5000pt]1:网桥模式[size=10.5000pt]2:安装好vmtoos[size=10.5000pt]3:安装好yum[size=10.5000pt]4:安装好ss ...

  10. 关于OpenFileDialog的使用(转)

    OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所 ...