graph | Max flow
最大流算法,解决的是从一个起点到一个终点,通过任何条路径能够得到的最大流量。
有个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的更多相关文章
- 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 ...
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
随机推荐
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Liz Murray成功故事的偶然与必然(转)
偶尔,我看到了一部电影:Homeless to Harvard (风雨哈佛路) 说句老实话,她( Liz Murray 莉丝·默里 )的经历确实让人钦佩和学习! 下面我看到他人写的有这本电影的评论,觉 ...
- android之WakeLock机制浅析
转自:http://blog.sina.com.cn/s/blog_4ad7c2540101n2k2.html 应用程序耗电的实质,是所启用的硬件在消耗电量. 手机的耗电单元 CPU: 应用处理器( ...
- phpstorm 8 license key
Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...
- 动态调用Webservice 支持Soapheader身份验证(转)
封装的WebserviceHelp类: using System; using System.CodeDom; using System.CodeDom.Compiler; using System. ...
- 13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
如果在应用中,如果想要给app 添加模糊滤镜,可能第一想到的是第三方类库,比如 Win2d.lumia Imaging SDK .WriteableBitmapEx,不可否认,这些类库功能强大,效果也 ...
- hdu 4033 二分几何
参考:http://blog.csdn.net/libin56842/article/details/26618129 题意:给一个正多边形内点到其他顶点的距离(逆时针给出),求正多边形的边长 二分多 ...
- NDK的Paths and Symbols设定
开发NDK的时候,如果JNI文件找不到C和C++的支持类,然后报很多很多红叉号, 是因为没有设定“Paths and Symbol”.
- loj 1308(点双连通分量应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27016 题意:求一个连通图去掉任意一个点以及与它相连的边,图中的所 ...
- 如何修改HDFS的备份数
我这个是看别人发的,记录一下,解决办法如下: