Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 53640   Accepted: 20448

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

 
 
n是点数,m是边数
 
EK算法:
    O(n*m*m)
    效率最低,没有任何优化,直接吧标号法实现。
    算法思想:一直重复搜索增广路径的过程,知道不存在增广路径。而此处对于增广路径的搜索,则采用了邻接矩阵的方法,耗时比较大,使用邻接表会更清晰的表现出标号法的过程。
 //312K    0MS    C++    1400B    2014-05-03 17:50:04
//最大流模板题
#include<iostream>
#include<queue>
#define INF 0x7ffffff
#define N 205
using namespace std;
int flow[N],vis[N];
int g[N][N],father[N];
int maxflow,n,m;
int Min(int a,int b)
{
return a<b?a:b;
}
void EK(int s,int e)
{
queue<int>Q;
while(){
memset(vis,,sizeof(vis));
memset(flow,,sizeof(flow));
Q.push(s);
flow[s]=INF;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int v=;v<=n;v++){
if(!vis[v] && g[u][v]>){
vis[v]=;
father[v]=u;
Q.push(v);
flow[v]=Min(flow[u],g[u][v]);
}
}
if(flow[e]>){
while(!Q.empty()) Q.pop();
break;
}
}
if(flow[e]==) break;
for(int i=e;i!=s;i=father[i]){
g[father[i]][i]-=flow[e];
g[i][father[i]]+=flow[e];
}
maxflow+=flow[e];
}
}
int main(void)
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(g,,sizeof(g));
maxflow=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
EK(,n);
printf("%d\n",maxflow);
}
return ;
}

dinic算法:

O(n*n*m)(如果用bfs实现增广路径的话则是O(n*m*m),好像是另一个算法了)

效率比EK高,因为多了优化。

算法思想:重复搜索增广路径的动作,而判定条件改为直到不能分层为止,能避免很多不必要的过程。(因为偷懒所以用了STL实现队列和邻接表)

 //15MS    340K    1496 B    G++
#include<iostream>
#include<queue>
#include<vector>
#define N 205
#define INF 0x7ffffff
using namespace std;
struct node{
int v,c;
node(int a,int b){
v=a;c=b;
}
};
int n,m;
vector<node>V[N];
int dis[N];
queue<int>Q;
bool bfs() //分层
{
memset(dis,-,sizeof(dis));
dis[]=;
int u=;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
for(int i=;i<V[u].size();i++){
if(dis[V[u][i].v]==- && V[u][i].c>){
dis[V[u][i].v]=dis[u]+;
Q.push(V[u][i].v);
}
}
}
return dis[n]>=;
}
int dfs(int u,int minn) //增广
{
if(u==n) return minn;
for(int i=;i<V[u].size();i++){
if(dis[V[u][i].v]==dis[u]+ && V[u][i].c>){
int c=V[u][i].c;
minn=minn<c?minn:c;
int ans=dfs(V[u][i].v,minn);
if(ans>){
V[u][i].c-=ans;
return ans;
}
}
}
return ;
}
int dinic()
{
int ans=;
while(bfs()){
ans+=dfs(,INF);
//printf("*%d\n",ans);
}
return ans;
}
int main(void)
{
int u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i=;i<=n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
V[u].push_back(node(v,c));
}
printf("%d\n",dinic());
}
return ;
}

ISAP:

O(n*n*m)

算法思想:这个算法看的时间比较长,因为比较难找到合理化的解释,第一次基本是套用别人的模板,现在写一下自己的理解。

算法复杂度上的分析我理解的不是很透彻,因此先借用别人的结果。而ISAP算法就分为几部分,开始先逆向BFS初始化距离标号,然后进入ISAP主函数,和标号法的思想差不多,(1)一直循环查找增广路径;(2)找到增广路径时则进行augment()处理,找不到时则回退retreat()处理;(3)当出现断层或则d[source]>=n时算法结束。

推荐一个网站:http://blog.csdn.net/yuhailin060/article/details/4897608

伪代码:

 algorithm Improved-Shortest-Augmenting-Path
f <--
从终点 t 开始进行一遍反向 BFS 求得所有顶点的起始距离标号 d(i)
i <-- s
while d(s) < n do
if i = t then // 找到增广路
Augment
i <-- s // 从源点 s 开始下次寻找
if Gf 包含从 i 出发的一条允许弧 (i,j)
then Advance(i)
else Retreat(i) // 没有从 i 出发的允许弧则回退
return f procedure Advance(i)
设 (i,j) 为从 i 出发的一条允许弧
pi(j) <-- i // 保存一条反向路径,为回退时准备
i <-- j // 前进一步,使 j 成为当前结点 procedure Retreat(i)
d(i) <-- + min{d(j):(i,j)属于残量网络Gf} // 称为重标号的操作
if i != s
then i <-- pi(i) // 回退一步 procedure Augment
pi 中记录为当前找到的增广路 P
delta <-- min{rij:(i,j)属于P}
沿路径 P 增广 delta 的流量
更新残量网络 Gf

题目解决的实现:

邻接矩阵:

 //15MS    532K    2612 B    G++
#include<stdio.h>
#include<string.h>
#define N 202
#define inf 0x7fffffff
int n,m,source,sink;
int g[N][N],f[N][N];
int pi[N]; //增广路径
int curnode[N]; //当前节点,优化循环 int queue[N]; //队列 int d[N]; //距离标号
int num[N]; //num[i] 为 d[j]=i 的数量,记录是否断层,断层结束算法 int bfs() //逆向bfs初始化距离标号
{
int head=,tail=;
for(int i=;i<=n;i++)
num[d[i]=n]++; num[n]--;
d[sink]=;
num[]++; queue[++tail]=sink; while(head!=tail){
int u=queue[++head]; for(int i=;i<=n;i++){
if(d[i]<n || g[i][u]==) continue; queue[++tail]=i; num[n]--;
d[i]=d[u]+;
num[d[i]]++;
}
}
return ;
} int augment() //处理增广路径
{
int width=inf;
for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
if(g[j][i]<width) width=g[j][i];
} for(int i=sink,j=pi[i];i!=source;i=j,j=pi[j]){
g[j][i]-=width; f[j][i]+=width;
g[i][j]+=width; f[i][j]-=width;
} return width;
} int retreat(int &i) //处理回退,重标号
{
int temp;
int mind=n-;
for(int j=;j<=n;j++)
if(g[i][j]> && d[j]<mind)
mind=d[j]; temp=d[i]; num[d[i]]--;
d[i]=mind+;
num[d[i]]++; if(i!=source) i=pi[i]; return num[temp];
} int maxflow(int s,int e) //ISAP求最大流
{
int i,j,flow=; source=s;
sink=e;
bfs();
//for(int i=1;i<=n;i++) printf("*%d\n",d[i]);
for(i=;i<=n;i++) curnode[i]=; i=source; for( ; d[source]<n ; ){ //主循环
for(j=curnode[i];j<=n;j++) //查找允许弧
if(g[i][j]> && d[i]==d[j]+)
break;
if(j<=n){ //存在允许弧
curnode[i]=j;
pi[j]=i;
i=j; if(i==sink){ //找到增广路径
flow+=augment();
//printf("*%d %d\n",pi[i],flow);
i=source;
} }else{ //不存在允许弧
curnode[i]=;
if(retreat(i)==) break;
}
}
return flow;
} int main(void)
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(g,,sizeof(g));
memset(f,,sizeof(f));
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
g[a][b]+=c;
}
printf("%d\n",maxflow(,n));
}
return ;
}

邻接表:

 //15MS    320K    1811 B    G++
#include<iostream>
#define N 1005
#define inf 0x7fffffff
using namespace std;
struct node{
int v;
int c;
int next;
}edge[N]; int head[N],eid;
int n,m; int h[N]; //距离标号
int gap[N]; //记录是否断层 void addedge(int u,int v,int c) //有向图
{
edge[eid].v=v;
edge[eid].c=c;
edge[eid].next=head[u];
head[u]=eid++;
edge[eid].v=u;
edge[eid].c=;
//edge[eid].c=c; //有向图
edge[eid].next=head[v];
head[v]=eid++;
}
int dfs(int u,int tc)
{
if(u==n) return tc; //找到增广路径
int c0;
int minh=n-;
int temp=tc;
for(int i=head[u];i!=-;i=edge[i].next){ //遍历与u有关的弧
int v=edge[i].v;
int c=edge[i].c;
if(c>){
if(h[v]+==h[u]){ //可行弧
c0=temp<c?temp:c;
c0=dfs(v,c0);
edge[i].c-=c0;
edge[i^].c+=c0;
temp-=c0;
if(h[]>=n) return tc-temp;
if(temp==) break;
}
if(h[v]<minh) minh=h[v]; //更新最短距离标号
}
}
if(temp==tc){ //不存在可行弧,回退操作
--gap[h[u]];
if(gap[h[u]]==) h[]=n; //出现断层
h[u]=minh+;
++gap[h[u]];
}
return tc-temp;
}
int ISAP() //isap算法
{
int ans=;
memset(gap,,sizeof(gap));
memset(h,,sizeof(h));
gap[]=n;
while(h[]<n){
ans+=dfs(,inf);
}
return ans;
}
int main(void)
{
int u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(head));
eid=;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
printf("%d\n",ISAP());
}
return ;
}

poj 1273 && hdu 1532 Drainage Ditches (网络最大流)的更多相关文章

  1. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  2. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93

    最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...

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

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

  5. hdu 1532 Drainage Ditches (最大流)

    最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...

  6. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  8. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

随机推荐

  1. 上海Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  3. python 解决url编译

    from urllib import parse s = parse.unquote("%7B%22name%22%3A%22joe%22%2C%22age%22%3A%2223%22%7D ...

  4. Express 总结

    Express Express提供了一个轻量级模块,把nodejs的http功能封装在一个简单易用的接口中.Express也扩展了http模块的功能,能轻松处理服务器的路由.响应.cookie和HTT ...

  5. 说说NSCache优于NSDictionary的几点

    1.NSCache可以提供自动删减缓存功能,而且保证线程安全,与字典不同,不会拷贝键.2.NSCache可以设置缓存上限,限制对象个数和总缓存开销.定义了删除缓存对象的时机.这个机制只对NSCache ...

  6. 01-JVM内存模型:程序计数器

    一.JVM模型概述 java虚拟机(JVM)在java程序运行的过程中,会将它所管理的内存划分为若干个不同的数据区域,这些区域有的随着JVM的启动而创建,有的随着用户线程的启动和结束而建立和销毁.一个 ...

  7. OSG-阴影

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  8. 怎么设计好移动APP测试用例

    软件测试工作中我们需要不断的储备和总结自己的知识和经验,怎么设计好移动APP测试用例?如:手机.平板.智能设备,并在特定网络环境下. 我们需要关注的功能点,容易出错的位置,这将对我们整个测试过程起着至 ...

  9. openjudge-2的100次方阶乘

    开始进行的第一天 #include <stdio.h> #include <string.h> int main() { int n; scanf("%d" ...

  10. 使用getid3获取音频文件信息

    今天有个需求,在上传音频文件时候自动获取音频的秒数,和大家分享一下. 首先把getid3的包下载下来 链接:https://pan.baidu.com/s/1Qmdj-I4boz9Sm9GFsON0D ...