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. CSS3 文本超出后显示省略号...

    纯用CSS实现,主要采用代码 overflow:hidden; text-overflow:ellipsis;//这是让文本溢出后,显示成省略号. white-space:nowrap;//禁止自动换 ...

  2. 如何解决svn Authorization failed错误

    出现这种问题肯定是SVN服务器出现了问题,需要修改其三个配置文件: 1.svnserve.conf: [general] anon-access = read auth-access = write  ...

  3. iOS笔记之内存泄露

    非ARC中,对于被autorelease的对象,Leak工具也会视其为泄露,自己知道没问题就行. 今天遇到一个bug,App在XCode调试时没有问题,但在真机安装,退出,再进入时,会出现闪退. 用X ...

  4. CUDA库函数使用笔记与案例(一)

    项目合作中需要整合对方公司提供的CUDA代码,因此需要详细学习代码中涉及的cuda函数. CUDA Tool Kit 8.0较完整的官方说明文档: http://docs.nvidia.com/cud ...

  5. Django中间件(勾子函数)使用

    中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django ...

  6. sar工具使用详细介绍

    一:命令介绍:参考资料:http://linux.die.net/man/1/sar sar(System ActivityReporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工 ...

  7. currentTarget,this,target区别

    currentTarget  : 事件处理程序当前正在处理事件的那个元素 this : 当前的事件发生的元素 target : 事件的目标 currentTarget和this值是始终相等的,但是ta ...

  8. JAVA怎么在函数内改变传入的值

    public class TestInt { public int aa(int i) { return i+4; } public static void main(String [] args) ...

  9. Linux服务器运行环境搭建(二)——Redis数据库安装

    官网地址:http://redis.io/ 官网下载地址:http://redis.io/download 1. 下载Redis源码(tar.gz),并上传到Linux 2. 解压缩包:tar zxv ...

  10. day3 文件系统 内核模块 ctags

    nfs网络文件系统 smb   修改配置文件  sudo  vim /etc/samba/smb.conf    重启服务   /etc/init.d/samba restart 自制小的文件系统 1 ...