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

Source

网络流咩,看了好久了,今天一并刷了
E-K算法重复寻找源点s到汇点t之间的增广路径,若有。找出增广路径上每一段[容量-流量]的最小值d,若无,则结束。在寻找增广路径时,能够用BFS来找。而且更新残留网络的值(涉及到反向边)。
而找到d后。则使最大流(maxflow)值加上d,更新为当前的最大流值

<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=0x7ffffff;
const int maxn=220;
int N,M;
int r[maxn][maxn];
int pre[maxn];
bool visit[maxn];
bool bfs(int s,int t)
{
queue<int>q;
memset(pre,-1,sizeof(pre));
memset(visit,false,sizeof(visit)); pre[s]=s;
visit[s]=true;
q.push(s); int p;
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=1;i<=M;i++)
{
if(r[p][i]>0&&!visit[i])
{
pre[i]=p;
visit[i]=true;
if(i==t)
return true;
q.push(i);
}
}
}
return false;
}
int solve(int s,int t)
{
int d,maxflow=0;
while(bfs(s,t))
{
d=INF;
for(int i=t;i!=s;i=pre[i])
d=min(d,r[pre[i]][i]);
for(int i=t;i!=s;i=pre[i])
{
r[pre[i]][i]-=d;
r[i][pre[i]]+=d;
}
maxflow+=d;
}
return maxflow;
}
int main()
{
while(cin>>N>>M)
{
memset(r,0,sizeof(r));
int s,e,c;
for(int i=0;i<N;i++)
{
cin>>s>>e>>c;
r[s][e]+=c;
}
cout<<solve(1,M)<<endl;
}
return 0;
}


Dinic算法:依据残留网络计算层次图,在层次图中进行DFS增广。

详见:Comzyh的博客(凝视具体,解说易懂)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int mp[250][250];
int dis[250];
int q[2000],h,r;
int n,m,ans;
int bfs()
{
int i,j;
memset(dis,-1,sizeof(dis));
dis[1]=0;
h=0;r=1;
q[1]=1;
while(h<r)
{
j=q[++h];
for(i=1;i<=n;i++)
{
if(dis[i]<0&&mp[j][i]>0)
{
dis[i]=dis[j]+1;
q[++r]=i;
}
}
}
if(dis[n]>0) return 1;
else return 0;
}
int find(int x,int low)
{
int a;
if(x==n) return low;
for(int i=1;i<=n;i++)
{
if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,mp[x][i]))))
{
mp[x][i]-=a;
mp[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int flow,tans;
int s,t;
while(~scanf("%d%d",&m,&n))
{
memset(mp,0,sizeof(mp));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&flow);
mp[s][t]+=flow;
}
ans=0;
while(bfs())
{
if(tans=find(1,0x7ffffff))
ans+=tans;
}
printf("%d\n",ans);
}
return 0;
}


POJ 1273 Drainage Ditches(网络流,最大流)的更多相关文章

  1. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  2. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  3. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  4. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  5. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  6. poj 1273 Drainage Ditches(最大流,E-K算法)

    一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...

  7. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  8. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  9. POJ 1273 Drainage Ditches【最大流】

    题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去 看的紫书的最大流,还不是很理解,照着敲了一遍 #include< ...

  10. POJ 1273 Drainage Ditches【最大流模版】

    题意:现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条有向水渠,给出这n条水渠所连接的点和所能流过的最大流量,求从源点到汇点能流过的最大流量 Dinic #include<iost ...

随机推荐

  1. 神奇的矩阵 NOI模拟题

    神奇的矩阵 题目大意 有一个矩阵\(A\),第一行是给出的,接下来第\(x\)行,第\(y\)个元素的值为数字\(A_{x-1,y}\)在\(\{A_{x-1,1},A_{x-1,2},A_{x-1, ...

  2. kingso_module - Taocode

    kingso_module - Taocode 模块介绍 Merger 功能介绍 Merger的功能: 合并多台Searcher机器的部分查询结果,得到最终的完整查询结果 向Detail集群请求最终展 ...

  3. .yml是什么文件

    YAML(IPA: /ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达资料序列的编程语言.YAML参考了其他多种语言,包括:XML.C语言.Python.Perl以及电子邮件格式RFC ...

  4. BZOJ 2795: [Poi2012]A Horrible Poem( hash )

    ...字符串hash. 假如长度x是一个循环节, 那么对于任意n(x | n)也是一个循环节. 设当前询问区间[l, r]长度为len = ∏piai, 最终答案ans = ∏piai' ,我们只需枚 ...

  5. selenium中用js定位html上没有id,没有name的元素

    所测试的页面包含display:none的元素,所以考虑用js,在使用js的过程中,js要获得页面的对象,没有id,没有name ...... <a class="employer&q ...

  6. Linux -FHS 标准

    FHS (Filesystem Hierarchy Standard),其目的是让用户可以了解以安装软件通常放在那个文件下面. /bin 放置的是单用户维护模式下,能够被调用的命令.主要有cat,ch ...

  7. javascript笔记整理(事件)

    一.事件驱动 1.事件javascript侦测到的用户的操作或是页面的一些行为(怎么发生的) 2.事件源引发事件的元素(发生在谁的身上) 3.事件处理程序对事件处理的程序或是函数 (发生了什么事) 二 ...

  8. oracle 表复制

    1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: ; 或者 create tab ...

  9. MinGW开发工具的安装

    MinGW是Minimalist GNU for Windows的缩写,是把linux下的GNU开发工具包移植到windows的项目之一.和Cygwin不一样的是,MinGW不提供linux的posi ...

  10. 【iOS发展-53】实例探究:scrollView使用方法和解决方案无法滚动核心

    案例效果: (1)基本的就是练习scrollView的使用方法.界面里面的其它元素基本都是UIView和UIButton堆砌起来的. (2)主要用代码实现.当然,能够先用storyboard拖个scr ...