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. DIV+ul+LI实现表格效果以及div带滑动条

    写这个是为了给自己一个好好的笔记,以免下次需要的时候又到处找,费神费事费时费力.开始吧! 1.先看看效果 2.网页代码 <!DOCTYPE html PUBLIC "-//W3C//D ...

  2. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  3. C++复习6.C/C++高级数据类型

    C/C++高级数据类型 1.C语言支持把基本数据类型组合起来形成更加强大的构造数据类型,就是C语言的struct(UDT, User Defined Type). Struct 和class : 在C ...

  4. MySQL Block Nested-Loop Join(BNL)

    5.5 版本之前,MySQL本身只支持一种表间关联方式,就是嵌套循环(Nested Loop).如果关联表的数据量很大,则join关联的执行时间会非常长.在5.5以后的版本中,MySQL通过引入BNL ...

  5. 201621123006 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 多态.重载.继承.覆盖.super.抽象类 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. ...

  6. New Concept English there (8)

    31w/m 56% The Great St Bernard Pass connects Switzerland to Italy. At 247o metres, it is the highest ...

  7. angular 与jQuery混用 大坑一

    由于angular是在真实dom加载之后,运行,所以jQuery操作dom时,要先于angular添加的元素(如,ng-repeat),所以常常会出现,元素错位,解决办法:添加一个透明的站位元素:

  8. SQL 添加删除列

    --添加一列 alter table TableName add columnName columnType --删除表中的一列 alter table TableName drop column c ...

  9. vim 强大复制链接

    参考文献: http://blog.csdn.net/xiyuan1999/article/details/5680102 vi编辑器中的整行(多行)复制与粘贴就非常必要了. 1.复制 1)单行复制 ...

  10. Git详解之四 服务器上的Git

    以下内容转载自:http://www.open-open.com/lib/view/open1328069988843.html 服务器上的 Git 到目前为止,你应该已经学会了使用 Git 来完成日 ...