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

有个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. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  2. curl请求的url中含有空格

    curl请求的url中含有空格时(例如rul的参数是sql查询语句,url=www.tets.com/query.php?sql=select * from t1),curl_easy_perform ...

  3. July 16th, Week 29th Saturday, 2016

    A long road tests a horse's strength and a long task proves a man's heart. 路遥知马力,日久见人心. Do you have ...

  4. Lucene查询索引(分页)

    分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...

  5. PHP常用类型判断函数

    1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...

  6. mac上安装gradle

    首先,先download最新版本的gradle,网址如下:http://www.gradle.org/get-started然后将下载下来的zip包放在你要安装的路径上,我安装在/usr/local/ ...

  7. 开源的DevOps开发工具箱

    DevOps是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和质量保障(QA)部门之间的沟通.协作与整合.在DevOps的整个流程中,使用一些开源工具可以促进开发与运维之间的 ...

  8. Ninja - chromium核心构建工具

    转自:http://guiquanz.me/2014/07/28/a_intro_to_Ninja/ Ninja - chromium核心构建工具Jul 28, 2014 [在线编辑] 缘由 经过上次 ...

  9. Web service project中导入的库JAX-RS(Java EE 6.0新产品)

    JAX-RS是JAVA EE6 引入的一个新技术. JAX-RS即Java API for RESTful Web Services,是一个Java 编程语言的应用程序接口,支持按照表述性状态转移(R ...

  10. HDU 4343 贪心

    D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...