最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎·····

希望慢慢地能够理解一点吧!

#include<stdio.h>

#include<string.h>

#include<queue>

using namespace std;

#define inf 1000000

#define min(a,b) a<b?a:b

int map[210][210],pre[210],maxf[210];

int n,m;

int bfs()//搜索,截止找不到增广路

{


int i;


queue<int>q;


for(i=0;i<=m;i++)


{


maxf[i]=inf;


pre[i]=-1;


}


pre[1]=0;


q.push(1);


while(!q.empty())


{


int qian=q.front();


q.pop();


int hou;


for(hou=1;hou<=m;hou++)


{


if(map[qian][hou]&&pre[hou]==-1)


{


pre[hou]=qian;


maxf[hou]=min(maxf[qian],map[qian][hou]);


q.push(hou);


}


}


}


if(pre[m]==-1)return 0;


return maxf[m];

}

int ek()//更新与修复

{


int max=0,kejia;


while(kejia=bfs())


{


max+=kejia;


int index=m,qian;


while(index!=1)


{


qian=pre[index];


map[qian][index]-=kejia;


map[index][qian]+=kejia;


index=qian;


}


}


return max;

}

int main()

{


int i,u,v,w,ans;


while(scanf("%d%d",&n,&m)!=EOF)


{


memset(map,0,sizeof(map));


for(i=0;i<n;i++)


{


scanf("%d%d%d",&u,&v,&w);


map[u][v]+=w;


}


ans=ek();


printf("%d\n",ans);


}


return 0;

}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
网络流 定义:流网络G(V,E)是一个V有向图,其中S为源点,T为汇点。 定义:C(u,v)为u到v的容量

,其中对于每条边(u,v)∈E,有C(u,v)≥0。否则C(u,v)=0。

定义:(u,v)为u到v的流量,对所有u,v∈V, 有f(u,v)≤c(u,v)。∑f(u,v)称作网络的流

记作

f(S,T) 定义:max(∑f(u,v))=max(f(S,T))为网络的最大流量。记住|f(s,t)|

残留网络

定义:Cf(u,v)为u到v的残留容量, Cf(u,v)=C(u,v)-f(u,v)。

定义:残留网络Ef={(u,v)∈VXV,Cf(u,v)}石油残留边组成的网络。

定义:增广路径是起点为S,终点为T的一组边集,其中Cf(p)=min{cf(u,v):(u,v)∈p}称为增广路径的容量。

最大流最小割定义:割(S,T)将网络分成两部分,割得流等于∑c(u,v) ,(u∈S,v∈T) 记作c(S,T)。

明显f(S,T)≤c(S,T),我们以后用c(S,T) 表达最小割。

定理:若残留网络中不存在增广路,则当前流为最大流

定理:最大流等于最小割

证明:

假设残留图

Gf

不存在增广路径,根据以下规则划分两个点集合S={v∈V:Gf 存在从s到v的路径}

T={v∈V:v∉S}

因为Gf不存在增广路,所以t ∉S, 对顶点u,v, 若u∈S,f(u,v)=c(u,v),则v属于T,否则v属于S,

此时f(S,T)=C(S,T),

f(S,T)<=C(S,T) 所以f(S,T)为最大流,此时残留图中无增广路

 

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 Dit ...

  3. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  4. HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...

  5. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

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

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

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

  7. HDU 1532 Drainage Ditches (网络流)

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

  8. 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) ...

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

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

随机推荐

  1. javaScript如何跳出多重循环break、continue

    先来说说break和continue之间的区别 for(var i=0;i<10;i++){  if(i>5){  break;  }}console.log(i);  ---6  •当i ...

  2. sad 关于一些html5新属性还需要用https才能支持

    像我昨天在搞一个录音的小东西 在本地正常录音正常播放 但是放到线上环境http环境上就出现了如上的错误 功能都不能正常使用 然后就改成https线上环境  然后就正常了 如上 大家有什么赐教的欢迎留言 ...

  3. webpack编译时No PostCSS Config的解决方法

    1. { loader:"postcss-loader", options: { // 如果没有options这个选项将会报错 No PostCSS Config found pl ...

  4. LeetCode862. Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  5. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  6. spring boot 使用不同的profile来加载不同的配置文件

    在开发过程之中,经常需要在开发和测试环境中进行互相切换,当切换的同时需要加载相应的配置文件,因此要经常 性的对配置文件进行相应的修改,长此以往感到十分痛苦.如果能针对开发和测试环境分别建两个不同的配置 ...

  7. 给你一个 5L 和 3L 桶,水无限多,怎么到出 4L。

    智力题 给你一个 5L 和 3L 桶,水无限多,怎么到出 4L. 思考过程 先将 3L 的桶装满水,倒入 5L 的桶里. 再重新将 3L 的桶装满水,倒入 5L 的桶里,把 5 L 的桶装满后,这样 ...

  8. cordova 导致css中绝对定位top:0会被顶到视图之外

    IOS7+ webview全屏导致状态栏悬浮在页面上 解决方案:打开 ios项目/classes/MainViewController.m,修改viewWillAppear方法 - (void)vie ...

  9. Es6懒加载

    const Login = resolve => require(['@/components/Login'], resolve) 注(当路由被访问的时候才加载这个组件)

  10. OOD沉思录 --- 类和对象的关系 --- 使用关系

    使用关系 对象A的方法MethodA使用了B的方法MethodB,则表示A对B存在使用关系 使用关系的最关键问题在于,A如何找到B,存在6种方案 方案一: A包含了B,B作为一个成员定义在A的类中,那 ...