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. HDU 3923 Invoker(polya定理+乘法逆元(扩展欧几里德+费马小定理))

    Invoker Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 122768/62768K (Java/Other) Total Subm ...

  2. gitblit-禁用用户

    使用管理员账号(此处演示使用admin)登录到系统 右上角,找到用户菜单 再用户列表,找到要进行权限管理的用户,(演示alex) 禁用用户,取消权限 验证已经禁用 pull 权限被禁止

  3. new/delete工作机制

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  4. Spring AOP的使用报错!

    用构造方法注入的时候 需要把无参构造方法手动加上 AOP底层会调用无参构造方法. 不加则报错:Superclass has no null constructors but no arguments ...

  5. 海思arm平台AAC音频转码cpu占用高、效率低的问题解决

    问题背景 目前市面上的大部分IPC摄像机音频输出基本都是G711.G726编码格式,而在类似于<基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控>这种业务中,都是 ...

  6. Vue Devtools--vue调式工具

    当浏览器控制台出现:Download the Vue Devtools extension for a better development experience: 1:安装 地址:https://g ...

  7. learn go return fuction

    package main // 参考文章: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.9.md im ...

  8. js之吸顶效果

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 让svn具有分布式的功能。

    最近开发遇到了个难事.公司的svn库不能随便提交,必须要经过验证.但是平时修改太多,如果不提交到svn说不定前面被删掉的代码后面又需要了.svn自带的relocate和switch都不能达到要求.找遍 ...

  10. 线性回归 Linear regression(4) 局部加权回归

    这篇文章将介绍过拟合和欠拟合的概念,并且介绍局部加权回归算法. 过拟合和欠拟合 之前在线性回归中,我们总是将单独的x作为我们的特征,但其实我们可以考虑将,甚至x的更高次作为我们的特征,那么我们通过线性 ...