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

默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念。比较好!一个寄出的网络最大流。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. JDBC 数据库连接 Java操作数据库 jdbc快速入门

    JDBC基本概念 Java DataBase Connectivity 数据库连接 java操作数据库 本质上(sun公司的程序员)定义的一套操作关系型数据库的规则 既接口  更新内容之前 代码 pa ...

  2. Java中常量定义在interface和class的区别(转)

    最终结论:定义常量在interface和class中其实都行,关键是看你的设计和个人爱好. Java中interface中定义变量默认都是"public static final" ...

  3. Regularized least-squares classification(正则化最小二乘法分类器)取代SVM

    在机器学习或者是模式识别其中有一种重要的分类器叫做:SVM .这个被广泛的应用于各个领域.可是其计算的复杂度以及训练的速度是制约其在实时的计算机应用的主要原因.因此也非常非常多的算法被提出来.如SMO ...

  4. [Javascript] Wrap fireEvent with fireEventAsync

    The idea is wrap a object with all its function methods and add some additional handling into a new ...

  5. 利用反射技术实现POJO的数据库操作

    记得第一次写项目的时候,傻傻的数据库一张表,代码里就写一个DAO类,几张表就写几个DAO类,大量的反复代码,自己粘着都嫌烦,后来接触了Hibernate,不得不说对我们这样的小白用处还是非常大的.那么 ...

  6. Android开发的环境搭建及HelloWorld的实现

    安装JDK和配置Java开发环境 http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-4321 ...

  7. 协议的注冊与维护——ndpi源代码分析

    在前面的文章中,我们对ndpi中的example做了源代码分析.这一次我们将尽可能深入的了解ndpi内部的结构和运作.我们将带着以下三个目的(问题)去阅读ndpi的源代码. 1.ndpi内部是怎么样注 ...

  8. 一场由股票提醒助手插件引发的血案——浅入浅出 jquery autocomplete

    我没有学过前端,所以这篇文章注定要班门弄斧了. 通常,须要用到什么技术什么语言时,我才去学,学了也不一定掌握,就是记不住!所以如今明确了.学习的时候,亦或是攻克难点的时候,一定要记录下来.并不一定非要 ...

  9. DW 表格与表单

    CSS样式表

  10. openwrt procd 运行的一些log

    void procd_inittab(void) { #define LINE_LEN 128 FILE *fp = fopen(tab, "r"); struct init_ac ...