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 173 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- PW试验-----verilog
PWM,脉冲宽度调制.顾名思义,是通过调制脉冲的宽度,即占空比,来实现的.可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象.若用LED灯来显示现象,则可以称作:LED呼 ...
- mysql 指定端口
mysql -P3307 -uemove -h180. -p #-P是指定端口
- MySQL常用简单小命令
1. 登录数据库: mysql -h localhost -u root -p
- php动态安装mongo扩展
首先下载mongo扩展包 http://pecl.php.net/package/mongo 开始安装把 wget http://pecl.php.net/get/mongo-1.5.8.tgz t ...
- Html页面插入flash代码
转自:http://www.educity.cn/jianzhan/402117.html 转自:http://www.cnblogs.com/yxc_fj/articles/1390621.html ...
- .net winform软件自动更新
转载自 http://dotnet.chinaitlab.com/DotNetFramework/914178.html 关于.NET windows软件实现自动更新,本人今天写了一个DEMO,供大家 ...
- POJ 2185 Milking Grid KMP(矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Lim ...
- 利用windbg探索进程和进程上下文
1.列出所有活动进程 使用!process命令可以打印出活动进程的信息.第一个参数是要打印的EPROCESS的地址,如果指定为0则表示打印所有的进程.第二个参数用于说明打印进程信息的详细级别.指定0则 ...
- hadoop1.2.1的namenode格式化失败的问题
最近要开始找工作,就在原来搭建好的hadoop1.2.1的伪分布式跑跑mapreduce 很久没用,就想着格式化一下namode,结果: Format aborted in /uar/local/ha ...