网络流是什么类型的问题,看一道题目你就知道了 点击打开链接 。

默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念。比较好!一个寄出的网络最大流。EK算法写的。

这是一幅网络,求S  ------>  T   点的最大网络流。  这是初始状态。{a,b}  a 代表该边的最大流量,b代表实际的流量。

一开始,实际流量为0;下面是代码。

<pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h> using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MM=250;
const double eps=0.0000000001; struct Node
{
int c,f;
};
Node edge[MM][MM];
int n,m,pre[MM]; bool bfs(int s,int t)
{
bool vis[MM];
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
queue<int>q;
q.push(s);
vis[s]=true;
while(!q.empty())
{
s=q.front();
q.pop();
for(int i=1; i<=m; i++)
{
if(!vis[i]&&edge[s][i].c>abs(edge[s][i].f))
{
vis[i]=true;
pre[i]=s;
if(i==t) return true;
q.push(i);
}
}
}
return false;
}
void print(stack<int>& a)//打印路径
{
cout<<"1";
while(!a.empty())
{
cout<<"->"<<a.top();
a.pop();
}
cout<<endl;
}
int solve()
{
int maxflow=0;
while(true)
{
if(!bfs(1,m)) return maxflow;
int flow=INF;
stack<int>s;
for(int x=m,y=pre[m]; y!=-1; x=y,y=pre[y])
{
flow=min(flow,edge[y][x].c-edge[y][x].f);
s.push(x);
}
// print(s);
for(int x=m,y=pre[m]; y!=-1; x=y,y=pre[y])
{
edge[y][x].f+=flow;
edge[x][y].f-=flow;
}
maxflow+=flow;
}
}
int main()
{
while(cin>>n>>m)
{
memset(edge,0,sizeof(edge));
for(int i=0; i<n; i++)
{
int s,t,val;
cin>>s>>t>>val;
edge[s][t].c+=val;
}
printf("%d\n",solve());
}
return 0;
} /*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The program have no BUG.
*/


该算法的大致思路是:不断的找寻s到t的可行流。直到找不到为止。每次找到都要之后改变该路径上的实际流量。

用广搜来寻找路径。

比如第一次找到的路径是    S   → 1  →2 →T;增加流量为4

把实际流量更新后得到

再找 S→1→4→T

这条路上S到1只剩下1 所以增加的流量为1 便得到

再找 S→3→4→1

可以增加的流量为 4  ,便得到

下一次将不会找到再可以增加流量的路径。所以程序结束,增加流量为     4+1+4;所以最大流为9;

不过还有一种情况:

从肉眼来看很容易 S→1→2→T     加      S→3→4→T  得到15;

但是计算机执行有可能会首选 S→1→4→T。便得到
 再选择  S→1→2→T得到右图

 
        

就再搜不到其他的了但是得到的确实10;说明这样作并不是最优的。所以EK算法提供了一种后悔的机制。正向流量加的同时,反向容量也同时加。这样就相当于
  让刚刚流经1到4的让他从1→2→T 我从 4→T

1与4
这条边,一开始是从1到4流过5,反方的流量就变为5;当另一个流经  S,3,4 时  就可以走1,2,T 。合起来就是

S→3→4→1→2→T
 又增加流量为5 到最后  E<1,4> 这条边总流量为 0;这种机制刚好解决了这个问题。

OVER

最大网络流 EK 算法的更多相关文章

  1. POJ 1459 网络流 EK算法

    题意: 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条:(0,1)20 (1,0) ...

  2. 网络流EK算法模板

    \(EK\)算法的思想就是每一次找一条增广路进行增广. 注意几个点: 存图时\(head\)数组要设为\(-1\). 存图的代码是这样的: inline void add(int u, int v, ...

  3. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  4. Drainage Ditches(网络流(EK算法))

    计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...

  5. 网络流Ek算法

    例题:  Flow Problem HDU - 3549 Edmonds_Karp算法其实是不断找增广路的过程. 但是在找的过程中是找"最近"的一天增广路, 而不是找最高效的一条增 ...

  6. HDU 3549 基础网络流EK算法 Flow Problem

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit ...

  7. 网络流 EK算法模板。

    这篇博客讲得很好 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ...

  8. ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)

    基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...

  9. ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)

    题意有点难读懂 //网络流-EK算法-ACM计算机工厂-构图重点 //Time:0Ms Memory:208K #include <iostream> #include<cstrin ...

随机推荐

  1. HDU - 5974 A Simple Math Problem (数论 GCD)

    题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...

  2. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  3. 【mac】mac上安装JDK

    安装步骤就是在Oracle官网下载你想要的JDK版本下载,下载的时候同意协议即可 这里就给出jdk安装的位置 还有一点要注意的是,在指定JVM的位置的时候,需要指定到home目录下

  4. PHP网站渗透中的奇技淫巧:检查相等时的漏洞

    PHP是现在网站中最为常用的后端语言之一,是一种类型系统 动态.弱类型的面向对象式编程语言.可以嵌入HTML文本中,是目前最流行的web后端语言之一,并且可以和Web Server 如apache和n ...

  5. sklearn preprocessing data(数据预处理)

    参考: http://scikit-learn.org/stable/modules/preprocessing.html

  6. Java中的反射机制,利用反射访问私有

    利用反射,首先是Class对象的获取,之后是Method和Field对象的获取. 以Method为例,从文档中可以看到: getMethod()方法返回的是public的Method对象, 而getD ...

  7. [LeedCode OJ]#28 Implement strStr()

    [ 声明:版权全部,转载请标明出处,请勿用于商业用途.  联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/implem ...

  8. 李洪强iOS开发之-实现点击单行View显示和隐藏Cell

    李洪强iOS开发之-实现点击单行View显示和隐藏Cell 实现的效果:  .... ....

  9. html使用代码大全

    <DIV style="FONT-SIZE: 9pt">1)贴图:<img src="图片地址">1)首行缩进2格:<p styl ...

  10. 关于用HOOK拦截键盘的一些问题

    因为MSDN上说要这样做,所以我就这样做的,读懂MSDN是关键,下面来仔细阅读一下MSDN,看它到底是怎样描述的.阅读的时候我先给出原文,再进行自己的一些翻译或描述. 先看回调函数KeyboardPr ...