网络流————Edmonds-Karp 最短增广路算法


■求最大流的过程,就是不断找到一条源到汇的路径,然后构建残余网络,再在残余网络上寻找新的路径,使总流量增加,然后形成新的残余网络,再寻找新路径…..直到某个残余网络上找不到从源到汇的路径为止,最大流就算出来了。

■每次寻找新流量并构造新残余网络的过程,就叫做寻找流量的“增广路径”,也叫“增广”

现在假设每条边的容量都是整数  ,这个算法每次都能将流至少增加1

由于整个网络的流量最多不超过 图中所有的边的容量和C,从而算法会结束

复杂度

找增广路径的算法可以用dfs,复杂度为边数m+顶点数n ,Dfs 最多运行C次 ,所以时间复杂度为C*(m+n) =C* n^2 
这个算法实现很简单  
但是注意到在图中C可能会非常大

因此在每次增广的时候,选择从源到汇的具有最少边数的增广路径,即不是通过dfs寻找增广路径,而是通过bfs寻找增广路径。

这就是Edmonds-Karp 最短增广路算法 

已经证明这种算法的复杂度上限为nm^2 (n是点数,m是边数)


Drainage Ditches

POJ - 1273

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

赤裸裸的网络流题目。给定点数,边数,每条边的容量,以及源点,汇点,求最大流。


#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;int G[300][300];
int Prev[300]; //路径上每个节点的前驱节点
bool Visited[300];
int n, m; //m是顶点数目,顶点编号从1开始 1是源,m是汇, n是边数unsigned Augment(void)
{int v;int i;deque<int> q;memset(Prev, 0, sizeof(Prev));memset(Visited, 0, sizeof(Visited));Prev[1] = 0;Visited[1] = 1;q.push_back(1);bool bFindPath = false;//用bfs寻找一条源到汇的可行路径while (!q.empty()){v = q.front();q.pop_front();for (i = 1; i <= m; i++){if (G[v][i] > 0 && Visited[i] == 0){//必须是依然有容量的边,才可以走Prev[i] = v;Visited[i] = 1;if (i == m){bFindPath = true;q.clear();break;}elseq.push_back(i);}}}if (!bFindPath)return 0;int nMinFlow = 999999999;v = m;//寻找源到汇路径上容量最小的边,其容量就是此次增加的总流量while (Prev[v]){nMinFlow = min(nMinFlow, G[Prev[v]][v]);v = Prev[v];}//沿此路径添加反向边,同时修改路径上每条边的容量v = m;while (Prev[v]){G[Prev[v]][v] -= nMinFlow;G[v][Prev[v]] += nMinFlow;v = Prev[v];}return nMinFlow;
}int main(void)
{while (~scanf("%d%d", &n, &m)){//m是顶点数目,顶点编号从1开始int i, j, k;int s, e, c;memset(G, 0, sizeof(G));for (i = 0; i < n; i++){scanf("%d%d%d", &s, &e, &c);G[s][e] += c; //两点之间可能有多条边}unsigned int MaxFlow = 0;unsigned int aug;while (aug = Augment())MaxFlow += aug;cout << MaxFlow << endl;}return 0;
}

网络流—Edmonds-Karp 最短增广路算法(最大流)相关推荐

  1. 网络流之 最短增广路算法模板(SAP)

    数据输入格式:首先输入顶点个数n和弧数m,然后输入每条弧的数据.规定源点为顶点0,汇点为顶点n-1.每条弧的数据格式为:u,v,w,分别表示这条弧的起点,终点,容量.顶点序号从0开始. 代码: 1 # ...

  2. 最短增广路Isap算法 网络流

    最短增广路 请先理解 bfs的求增广路的算法,再来学习Isap算法 最短增广路Isap算法 图片来源 <趣学算法>人民邮电出版社 陈小玉 /* 最短可增广路:重贴标签算法Isap 算法设计 ...

  3. 网络最大流中一般增广路算法(标号法)

    网络最大流主要有两大类求解方法:增广路算法和预流推进算法 一般增广路算法:主要分为初始流为零流和初始流为非零流的情况!后者在标号的时候注意一条边是正向连接还是反向连接:若是反向的连接,那么在调整的时候 ...

  4. Sweet Snippet 之 最短增广路径算法

    本文简单实现了最短增广路径算法 首先我们简单实现 queue(队列) 数据结构 : local queue = {} queue.__index = queuefunction queue:push( ...

  5. 【网络流】解题报告:luogu P2740 [USACO4.2]草地排水Drainage Ditches(Edmonds-Karp增广路,最大流模板)

    题目链接:草地排水 若一条从源点到汇点的路径上各条边的剩余容量都大于0,则称这条路径为一条增广路. Edmonds-Karp增广路的策略就是不断用bfs寻找增广路,直至网络中不在存在增广路为止. 在每 ...

  6. 增广路算法 (最大流问题)

    Edmonds-Karp算法: 计算机科学中, Edmonds–Karp算法通过实现Ford–Fulkerson算法来计算网络中的最大流,其时间复杂度为O(V E2). 该算法由Yefim (Chai ...

  7. SAP和ISAP(网路最大流的两个增广路算法)

    ISAP是对SAP进行优化后的算法,ISAP时间复杂度为O(V^2E),SAP的时间复杂度为O(VE^2) SAP #include <iostream> #include <alg ...

  8. 网络流 之 一般增广路算法 标号法实现

    数据输入格式:首先输入顶点个数n和弧数m,然后输入每条弧的数据.规定源点为顶点0,汇点为顶点n-1.每条弧的数据格式为:u,v,c,f,分别表示这条弧的起点,终点,容量和流量.顶点序号从0开始. 代码 ...

  9. 网络流 增广路 回退

    增广路: 有流量标记: 流量变化: 转载于:https://www.cnblogs.com/cmyg/p/9568904.html

最新文章

  1. 你不需要jQuery(四)
  2. SAP HR模块用的表
  3. python和node_Python和NodeJS绘图对比
  4. Git本地仓库文件的创建、修改和删除
  5. Iterator(迭代器)--对象行为模式
  6. 学习linux第二周作业
  7. this.$router.push用query传参对象时需注意的地方
  8. [转]测试的三重境界
  9. 【大数据部落】r语言使用rjags R2jags建立贝叶斯模型
  10. Guacamole之本地安装Guacamole(二)
  11. GIF图片批量改JPG格式
  12. Arduino+DHT11+OLED显示温湿度信息(附详细文档+源码)
  13. Factorization Machines 论文翻译
  14. 20110609 WindowsLive Writer插件 测试
  15. jquery.qrcode 批量打印
  16. Android Service---在前台运行服务
  17. testlink升级
  18. Python 条件判断 If
  19. maching learning入门(四)
  20. 百分数转bigd_英语口语小数、百分数以及倍数巧表达

热门文章

  1. mysql 匹配 findinset
  2. Mysql 基于 Amoeba 的 读写分离(2)
  3. 14条改善jquery代码的建议
  4. 据说是腾讯php程序员面试题目 蛋疼..要是提供PHP手册就HI了..
  5. Oracle使用手册(三)---存储过程与触发器
  6. scrum项目管理_Scrum,用于初创企业(或针对该项目的任何项目)
  7. 数据通信技术(一:IP划分)
  8. idea使用maven创建java工程log4j的配置
  9. (C++)1045 快速排序 非满分
  10. amh支持java吗_跟我学Android之三 常用视图