最大网络流 EK 算法
网络流是什么类型的问题,看一道题目你就知道了 点击打开链接 。
默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念。比较好!一个寄出的网络最大流。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 算法的更多相关文章
- 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) ...
- 网络流EK算法模板
\(EK\)算法的思想就是每一次找一条增广路进行增广. 注意几个点: 存图时\(head\)数组要设为\(-1\). 存图的代码是这样的: inline void add(int u, int v, ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
- Drainage Ditches(网络流(EK算法))
计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...
- 网络流Ek算法
例题: Flow Problem HDU - 3549 Edmonds_Karp算法其实是不断找增广路的过程. 但是在找的过程中是找"最近"的一天增广路, 而不是找最高效的一条增 ...
- HDU 3549 基础网络流EK算法 Flow Problem
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit ...
- 网络流 EK算法模板。
这篇博客讲得很好 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ...
- ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)
基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...
- ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)
题意有点难读懂 //网络流-EK算法-ACM计算机工厂-构图重点 //Time:0Ms Memory:208K #include <iostream> #include<cstrin ...
随机推荐
- Laravel 控制器的middleware中间件
场景:活动开始前只能访问宣传页面,开始后才可以访问其他页面: 步骤: 新建中间件, 注册中间件, 使用中间件, 中间件的前置和后置操作. 控制器: public function activity0( ...
- python(3)- 常用快捷键及基础命令
- max file descriptors [4096] for elasticsearch proess is too low, increase to at least [65536]
修改文件 /etc/security/limits.conf 加入以下两行: sonar hard nofile 65536 sonar soft nofile 65536 #备注:sonar这里是 ...
- HDU 5676 ztr loves lucky numbers【DFS】
题目链接; http://acm.hdu.edu.cn/showproblem.php?pid=5676 题意: 由4和7组成的且4和7出现次数相同的数称为幸运数字,给定n,求不大于n的最大幸运数字. ...
- 虚拟机centos 里tomcat的端口映射到主机 Windows里面
- Java_AOP原理
AOP : 面向切面编程 在程序设计中,我们需要满足高耦合低内聚,所以编程需满足六大原则,一个法则. AOP面向切面编程正是为了满足这些原则的一种编程思想. 一.装饰者模式: 当我们需要给对象增加功能 ...
- 将文件从已Root Android手机中copy出来的几个cmd窗口命令
将文件从已Root Android手机中copy出来的几个cmd窗口命令: 以shell身份登录adbadb shell进入adb后切换至root用户su更改文件的所属chown shell *更改文 ...
- JavaScript中判断变量类型最简洁的实现方法以及自动类型转换(#################################)
这篇文章主要介绍了JavaScript中判断整字类型最简洁的实现方法,本文给出多个判断整数的方法,最后总结出一个最短.最简洁的实现方法,需要的朋友可以参考下 我们知道JavaScript提供了type ...
- 30 分钟编写一个 Flask 应用
Flask 是一种很赞的Python web框架.它极小,简单,最棒的是它很容易学. 今天我来带你搭建你的第一个Flask web应用!和官方教程 一样,你将搭建你自己的微博客系统:Flaskr.和官 ...
- weexpack build android 和 weexpack run android 报错 及 解决方案
1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...