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. 最新的QT git代码到code.qt.io/cgit,还有planet.qt.io有许多博客

    http://code.qt.io/cgit/ http://planet.qt.io/

  2. myeclipse自动生成注释

    myeclipse自动生成注释 在使用Eclipse编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的,例如其中author的属性值. 我们可以在Eclipse中进行设置自己希望显示 ...

  3. lua序列化table表到文件中

    先上代码 function luautil.serialize(t, sort_parent, sort_child) local mark={} local assign={} local func ...

  4. 修改Hadoop作业调度算法过程解析

    最近几个星期一直在修改hadoop的计算能力调度算法,遇到了这样那样的问题. 我修改的版本是hadoop-0.20.2 第一步: 将hadoop的源码加载到eclipse中配置使用ant编译 第二步: ...

  5. arm:启动代码判断是从nand启动还是从norflash启动,拷贝程序到内存的过程

    一.nand启动和nor启动:[1] CPU从0x00000000位置开始运行程序. 1.nand启动: 如果将S3C2440配置成从NANDFLASH启动(将开发板的启动开关拔到nand端,此时OM ...

  6. BZOJ 1110: [POI2007]砝码Odw( 贪心 )

    ORZjcvb... #include<bits/stdc++.h> using namespace std; ; int N, M, item[maxn], V[maxn]; vecto ...

  7. CentOS6.5 服务器+apache5.3绑定多个域名+SELinux设置

    下面简单的介绍了如何通过设置Apache的http.conf文件,进行多个域名以及其相关的二级域名的绑定(假设我们要绑定的域名是minidx.com和ntt.cc,二级域名是blog.minidx.c ...

  8. 如何判断是否安装了VC RUNTIME

    先得说下GUID,它是Globally Unique Identifier的简称,中文翻译为“全球唯一标示符”,在Windows系统中也称之为Class ID,缩写为CLSID.对于不同的应用程序,文 ...

  9. IntelliJ IDEA 开发swing(二)

    原文:idea开发swing(二) 闲话少说,书接idea开发swing(一). 程序编译完成后,需要打包发布,如果有fat_jar的同学可以通过该插件打包,这里是使用ant来打包,步骤如下: 一.编 ...

  10. httplib,urllib和urllib2

    一.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib con ...