最大流算法,解决的是从一个起点到一个终点,通过任何条路径能够得到的最大流量。

有个Edmond-Karp算法:

1. BFS找到一条增广路径;算出这条路径的最小流量(所有边的最小值)increase;

2. 然后更新路径上的权重(流量),正向边加上increase,反向边减去increase;

3. 重复1,直到没有增广路径;

可以证明的是在使用最短路增广时增广过程不超过V*E次,每次BFS的时间都是O(E),所以Edmonds-Karp的时间复杂度就是O(V*E^2)。

图的BFS和DFS的时间复杂度都是O(n+e),这里指用邻接表的方式。每次出栈或者出队列,都要扫一遍该点的所有边,所有点的边集加起来就是O(e)了。

至于为什么要用反向边,这里讲得挺清楚;

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <vector>
using namespace std; class Maxflow {
public: Maxflow() {}
~Maxflow() {
if (pre) delete[] pre;
if (flow) delete[] flow;
if (weight) {
for (int i = ; i < vertices; ++i) {
delete[] weight[i];
}
delete[] weight;
}
}
void readGraph(string filename) {
freopen(filename.c_str(), "r", stdin);
int edges;
scanf("%d %d", &vertices, &edges);
pre = new int[vertices];
flow = new int[vertices];
memset(flow, , vertices * sizeof(int));
weight = new int*[vertices];
for (int i = ; i < vertices; ++i) {
weight[i] = new int[vertices];
memset(weight[i], , vertices * sizeof(int));
} for (int i = ; i < edges; ++i) {
int v1, v2, w;
scanf("%d %d %d", &v1, &v2, &w);
weight[v1 - ][v2 - ] = w;
}
} int maxFlow() {
int start = , end = vertices - ;
int max = ;
int increase = ;
while ((increase = bfs(start, end)) != ) {
int p = end;
while (p != start) {
int b = pre[p];
weight[b][p] -= increase;
weight[p][b] += increase;
p = b;
}
max += increase;
}
return max;
}
private:
int bfs(int start, int end) {
memset(pre, -, vertices * sizeof(int));
vector<vector<int> > layers();
int cur = , next = ;
layers[cur].push_back(start);
flow[start] = INT_MAX; while (!layers[cur].empty()) {
layers[next].clear();
for (int i = ; i < layers[cur].size(); ++i) {
int v1 = layers[cur][i];
for (int v2 = ; v2 < vertices; ++v2) {
if (weight[v1][v2] <= || pre[v2] != -) continue;
pre[v2] = v1;
layers[next].push_back(v2);
flow[v2] = min(flow[v1], weight[v1][v2]);
if (v2 == end) return flow[v2];
}
} cur = !cur;
next = !next;
}
return ;
}
int* pre;
int** weight;
int* flow;
int vertices;
}; int main() {
Maxflow maxflow;
maxflow.readGraph("input.txt");
cout<< maxflow.maxFlow() << endl;
return ;
}

sample input:


graph | Max flow的更多相关文章

  1. HackerRank "Training the army" - Max Flow

    First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...

  2. min cost max flow算法示例

    问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...

  3. [Luogu 3128] USACO15DEC Max Flow

    [Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...

  4. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

  5. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  6. Max Flow

    Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...

  7. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  8. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  9. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

随机推荐

  1. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. 【python】一个简单的贪婪爬虫

    这个爬虫的作用是,对于一个给定的url,查找页面里面所有的url连接并依次贪婪爬取 主要需要注意的地方: 1.lxml.html.iterlinks()  可以实现对页面所有url的查找 2.获取页面 ...

  3. 【python】2048

    来源:https://www.shiyanlou.com/courses/368 实验楼的2048程序,在linux下可实现通过终端游戏. 主要学习的知识点: 1.状态机函数实现,用字典将状态和函数相 ...

  4. 告别div,可以代替div的几个标签

    几个最常用的用来代替DIV的HTML5元素 虽说html5中大多数功能性的元素如<video><canvas><audio>等还得不到当前主流浏览器的支持(主要就是 ...

  5. python基础——使用list和tuple

    python基础——使用list和tuple list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用 ...

  6. TinyHttpd中sockaddr与struct sockaddr_in的区别

    上午学习TinyHttpd的源码,sockaddr 结构体以前没接触过, 在网络编程中经常用到struct sockaddr和struct sockaddr_in,这里简单介绍. 在linux环境下, ...

  7. ***CI中的数据库操作(insert_id新增后返回记录ID)

    在system/application/config 文件夹和里面的config文件里已经配置了参数 $active_group = "default";$db['default' ...

  8. Ubuntu16.04 安装openjdk-7-jdk

    Ubuntu16.04 安装openjdk-7-jdk sudo apt-get install openjdk-7-jre 或者sudo apt-get install openjdk-7-jdk ...

  9. [整理]android中几种常见的尺寸

    获取屏幕宽高尺寸的三种代码形式 在Android上,目前我知道的获取屏幕尺寸的方法有三种不同的代码形式 方法1.在Activity中最常见的调用方式 WindowManager windowManag ...

  10. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...