Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 54962   Accepted: 20960

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
#include"stdio.h"
#include"string.h"
#include"queue"
#include"stack"
#include"iostream"
#include"stdlib.h"
#define inf 99999999
#define M 50000
using namespace std;
struct st
{
int u,v,w,next;
}edge[M];
int t,head[M],cur[M],q[M],gap[M],dis[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++; edge[t].u=v;
edge[t].v=u;
edge[t].w=0;
edge[t].next=head[v];
head[v]=t++;
}
void bfs(int start,int endl)//建立到汇点的距离层次图存在dis[]数组中
{
int rear=0,i,j;
memset(dis,-1,sizeof(dis));
memset(gap,0,sizeof(gap));//gap[x]记录dis[i]=x出现了多少次
dis[endl]=0;
gap[dis[endl]]=1;
q[rear++]=endl;
for(i=0;i<rear;i++)
{
for(j=head[q[i]];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(dis[v]==-1)
{
++gap[dis[v]=dis[q[i]]+1];
q[rear++]=v;
}
}
}
}
int SAP(int start,int endl,int n)
{
int ans=0;
bfs(start,endl);
int cur[M];//代替head数组
memcpy(cur,head,sizeof(head));
int stack[M],top=0;//建立手工栈
int u=start,i;
while(dis[start]<n)
{
if(u==endl)//当搜到终点时即找到从原点到汇点的增光路,正常处理即可
{
int mini=inf,tep;
for(i=0;i<top;i++)
{
if(mini>edge[stack[i]].w)
{
mini=edge[stack[i]].w;
tep=i;
}
}
for(i=0;i<top;i++)
{
edge[stack[i]].w-=mini;
edge[stack[i]^1].w+=mini;
}
ans+=mini;
top=tep;
u=edge[stack[top]].u;//此时的u为变容量为0的u
}
if(dis[u]&&gap[dis[u]-1]==0)//出现了断层,没有增广路
break;
for(i=cur[u];i!=-1;i=edge[i].next)//遍历与u相连的未遍历的节点
{
int v=edge[i].v;
if(dis[v]!=-1)
{
if(edge[i].w&&dis[u]==dis[v]+1)//层次关系找到允许路径
break;
}
}
if(i!=-1)//找到允许弧
{
cur[u]=i;
stack[top++]=i;
u=edge[i].v;
}
else//无允许的路径,修改标号 当前点的标号比与之相连的点中最小的多1
{
int mini=n;
for(i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].w==0)continue;
int v=edge[i].v;
if(mini>dis[v])//找到与u相连的v中dep[v]最小的点
{
mini=dis[v];
cur[u]=i;//最小标号就是最新的允许弧
}
}
--gap[dis[u]];//dep[u] 的个数变化了 所以修改gap
++gap[dis[u]=mini+1];//将dep[u]设为min(dep[v]) + 1, 同时修改相应的gap[]
if(u!=start)//该点非源点&&以u开始的允许弧不存在,退点
u=edge[stack[--top]].u;
}
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=-1)
{
init();
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
int ans=SAP(1,n,n);
printf("%d\n",ans);
}
}

网络流SAP+gap+弧优化算法的更多相关文章

  1. poj 3469 最小割模板sap+gap+弧优化

    /*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...

  2. 网络流 - dinic + 当前弧优化【代码】

    这是初学网络流的时候从<算法竞赛进阶指南>抄下来的一份代码,自己理解的也不是很透彻. 注意,边要从 \(1\) 开始计,不然直接 \(xor\) 运算的话取反边会直接炸掉. #includ ...

  3. ARC085E(最小割规划【最大流】,Dinic当前弧优化)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;const ll inf=0x3f3f3f3f;int cn ...

  4. 【最大流之Dinic算法】POJ1273 【 & 当前弧优化 & 】

    总评一句:Dinic算法的基本思想比较好理解,就是它的当前弧优化的思想,网上的资料也不多,所以对于当前弧的优化,我还是费了很大的功夫的,现在也一知半解,索性就写一篇博客,来发现自己哪里的算法思想还没理 ...

  5. DINIC网络流+当前弧优化

    DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...

  6. P3376 网络流-最大流模板题(Dinic+当前弧优化)

    (点击此处查看原题) Dinic算法 Dinic算法相对于EK算法,主要区别在于Dinic算法对图实现了分层,使得我们可以用一次bfs,一次dfs使得多条增广路得到增广 普通的Dinic算法已经可以处 ...

  7. 解题报告:hdu 3572 Task Schedule(当前弧优化Dinic算法)

    Problem Description Our geometry princess XMM has stoped her study in computational geometry to conc ...

  8. [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]

    题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...

  9. 网络流--最大流--Dinic模板矩阵版(当前弧优化+非当前弧优化)

    //非当前弧优化版 #include <iostream> #include <cstdio> #include <math.h> #include <cst ...

随机推荐

  1. sql server merge 的用法

    CREATE TABLE tTable ( id INT , f1 VARCHAR(10) , f2 VARCHAR(10) , f3 VARCHAR(10) ) GO INSERT INTO tTa ...

  2. 第三百一十七节,Django框架,缓存

    第三百一十七节,Django框架,缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返 ...

  3. 64位程序,long*转long 出错

    原因: long*在64位程序中占8个字节,long占4个字节.强转会出错. 解决方法: 把long用long long替换,long long 占8个字节

  4. 把py文件打成exe

    使用pyinstaller: pyinstaller -F -w -i manage.ico demo.py -F:打包为单文件-w:Windows程序,不显示命令行窗口-i:是程序图标,demo.p ...

  5. 深入解析AsyncTask

    REFRENCES:http://blog.csdn.net/hitlion2008/article/details/7983449 AsyncTask的介绍及基本使用方法 关于AsyncTask的介 ...

  6. 资深投资人全力反击: VC增值平台从来就不是一坨狗屎

    编者注: 本文来自海外著名科技博客VentureBeat, 英文原文出自Kyle Lacy之手 ,中文版由天地会珠海分舵进行编译. 文章主要是针对前几天德国VC Christian Claussen的 ...

  7. LINUX 环境变量总结

    1.概述 Linux是一个多用户的操作系统.多用户意味着每个用户登录系统后,都有自己专用的运行环境.而这个环境是由一组变量所定义,这组变量被称为环境变量.用户可以对自己的环境变量进行修改以达到对环境的 ...

  8. 【Mongo】聚合函数

    http://blog.csdn.net/miyatang/article/details/20997313 SQL Terms, Functions, and Concepts MongoDB Ag ...

  9. Unity3D使用经验总结 编辑器扩展篇【转】

    一个引擎,最重要的就是工具,工具除了提升开发速度,提供可视化操作环境以外,还带了容错功能. 它使得大家的工作局限在一定的范围内,比如一个变量的配置,或者是一些类型的选择. 使用编辑器,使得既使不太明白 ...

  10. Windows中目录及文件路径太长无法删除的解决方法

    用windows自带的命令解决  win7以上的系统有 robocopy 命令 http://www.jianshu.com/p/95a269951a1b 导致目录太深的原因就是用node中的node ...