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

默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念。比较好!一个寄出的网络最大流。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. linux基础命令之一

    1.cpio cpio(copy in/out) 功能说明:备份文件. 语 法:cpio [-0aABckLovV][-C <输入/输出大小>][-F <备份档>][-H &l ...

  2. msp430项目编程60

    msp430综合项目---扩展项目八60 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  3. Oracle 12c在PDB中创建scott/tiger

    scott/tiger一直以来是oracle数据的默认用户,但是跟之前的版本相比,Oracle 12c引入了PDB管理,所以要麻烦一些 下面假设管理员为SYS/Oracle12csys,在orcl实例 ...

  4. Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)

    package test; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleD ...

  5. 解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题

    解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题 2.问题: 在一般的请求中,content-type为:application/x-www-form ...

  6. 自动调整文字高度With what should I replace the deprecated sizeWithFont:contrainedToSize:lineBreakMode method?

    自动调整文字的高度: ios 2.0 ~ 7.0以下: UILabel *orgnizationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ...

  7. python解析网页中js动态添加的内容

    https://www.cnblogs.com/asmblog/archive/2013/05/07/3063809.html https://www.zhihu.com/question/21471 ...

  8. 【转载】容器技术 & Docker & 与虚拟化的比较

    看到10月份天天写博客,只有一天没写,非常棒! 11月份也基本每天都写,现在看到有三天没加新博客,应该是之前挖的坑太多了,需要填坑,呵呵. 那这篇文章是不是为了占坑呢?哈哈.我不说话. 容器技术,这篇 ...

  9. HTML网页之进入站点口令脚本

    加入以下这个脚本在head标签中. <script language="JavaScript"> <!-- var password=""; ...

  10. centos 重新获取IP hdcp 模式

    centos 重新获取IP hdcp 模式 dhclient -r →release dhclient →renew