Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70333   Accepted: 27336

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

Dinic实现最大流.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
int arc[MAXN][MAXN];
int n,m;
int level[MAXN];
bool bfs(int src,int ter)
{
memset(level,-,sizeof(level));
queue<int> que;
que.push(src);
level[src]=;
while(!que.empty())
{
int u=que.front();que.pop();
for(int i=;i<=n;i++)
{
if(level[i]<&&arc[u][i]>)
{
que.push(i);
level[i]=level[u]+;
}
}
}
if(level[ter]>) return true;
else return false;
} int dfs(int u,int ter,int f)
{
if(u==ter) return f;
for(int i=;i<=n;i++)
{
int d;
if(arc[u][i]>&&level[i]==level[u]+&&(d=dfs(i,ter,min(arc[u][i],f))))
{
arc[u][i]-=d;
arc[i][u]+=d;
return d;
}
}
return ;
}
int max_flow(int src,int ter)
{
int ans=;
int d;
while(bfs(src,ter))
{
while(d=dfs(src,ter,INF)) ans+=d;
}
return ans;
}
int main()
{
// freopen("input.in","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(arc,,sizeof(arc));
for(int i=;i<m;i++)
{
int from,to,w;
scanf("%d%d%d",&from,&to,&w);
arc[from][to]+=w;
}
int res=max_flow(,n);
printf("%d\n",res);
}
return ;
}

POJ1273(最大流入门)的更多相关文章

  1. 使用Guava RateLimiter限流入门到深入

    前言 在开发高并发系统时有三把利器用来保护系统:缓存.降级和限流 缓存: 缓存的目的是提升系统访问速度和增大系统处理容量 降级: 降级是当服务出现问题或者影响到核心流程时,需要暂时屏蔽掉,待高峰或者问 ...

  2. JavaIo流入门篇之字节流基本使用。

    一 基本知识了解(  字节流, 字符流, byte,bit是啥?) /* java中字节流和字符流之前有接触过,但是一直没有深入的学习和了解. 今天带着几个问题,简单的使用字节流的基本操作. 1 什么 ...

  3. Java-io流入门到精通详细总结

    IO流:★★★★★,用于处理设备上数据. 流:可以理解数据的流动,就是一个数据流.IO流最终要以对象来体现,对象都存在IO包中. 流也进行分类: 1:输入流(读)和输出流(写). 2:因为处理的数据不 ...

  4. IO流入门-第十三章-File相关

    /* java.io.File 1.File和流无关,不能通过该类完成文件的读写 2.File是文件和目录路径名的抽象变现形式. */ import java.io.*; public class F ...

  5. IO流入门-第十二章-ObjectInputStream_ObjectOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例,序列化和反序列化 import java.io.Serializable; //该接口是一个“可序列化”的 ,没有任 ...

  6. IO流入门-第十一章-PrintStream_PrintWriter

    DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.Print ...

  7. IO流入门-第十章-DataInputStream_DataOutputStream

    DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.DataOutputStream 数据字节输出流,带着类型写入 可以将内存中的“int i = ...

  8. IO流入门-第九章-BufferedReader_BufferedWriter复制

    利用BufferedReader和BufferedWriter进行复制粘贴 import java.io.*; public class BufferedReader_BufferedWriterCo ...

  9. IO流入门-第八章-BufferedWriter

    BufferedWriter基本用法和方法示例 import java.io.*; public class BufferedWriterTest01 { public static void mai ...

  10. IO流入门-第七章-BufferedReader

    BufferedReader基本用法和方法示例 /* 字节 BufferedInputStream BufferedOutputStream 字符 BufferedReader:带有缓冲区的字符输入流 ...

随机推荐

  1. mysql和SqlServer 中取得汉字字段的各汉字首字母

    mysql 中取得汉字字段的各汉字首字母 这个转载于http://blog.csdn.net/lky5387/article/details/11973721 DELIMITER ;;CREATE  ...

  2. vue 错误记录

    1.错误信息: You may use special comments to disable some warnings.Use // eslint-disable-next-line to ign ...

  3. Ubuntu12.04下samba服务器共享配置

    1 . 前置工作 首先保证你的Ubuntu能上网:虚拟机网络连接方式为NAT:虚拟机雨物理机互ping可通: 2. 安装samba sudo apt-get insall samba sudo apt ...

  4. 剑指offer--44.两个链表的第一个公共结点

    @selfboot 牛逼的代码,长度相同,一遍出结果, 长度不同,短的点跑完,变成长的,当长的跑完变成短的链表的时候,较长的链表已经走过了多的结点. ------------------------- ...

  5. MetaPost使用

    简介 MetaPost是一种制图语言,由John D. Hobby开发. 如果你要学习它,可以去下面的网址看看. 官网:http://tug.org/metapost 权威手册:http://tug. ...

  6. Eclipse下搭建SWT与Swing图形界面开发环境

    一.SWT与Swing介绍 SWT(StandardWidget Toolkit)则是由Eclipse项目组织开发的一套完整的图形界面开发包,虽然当初仅仅是IBM为了编写Eclipse的IDE环境才编 ...

  7. React-Native进阶_6.导航 Naviagtion传递数据并展示

    接着上面 Navigation 继续学习传递数据给下一个页面 onPress={() => this.props.navigation.navigate('Detail',{info:movie ...

  8. 创建Azure Function

    azure function的用途在于运行一些逻辑简单的执行逻辑,比如batch job,定时任务,webhook等等.1. 创建azure function创建完毕后,进入app service,选 ...

  9. xcode好玩的条件断点设置. 可以真人发声。

    哈哈.设置个条件断点. 然后 Logmessage  or speak message 挺有意思. 一听就知道是哪个断点触发了.

  10. linux系统编程-进程

    进程 现实生活中 在很多的场景中的事情都是同时进行的,比如开车的时候 手和脚共同来驾驶汽车,再比如唱歌跳舞也是同时进行的: 如下是一段视频,迈克杰克逊的一段视频: http://v.youku.com ...