网络流一·Ford-Fulkerson算法
来源:互联网 发布:跟章泽天是校友 知乎 编辑:程序博客网 时间:2024/05/21 17:49
- 样例输入
6 71 2 31 3 52 4 13 4 23 5 34 6 45 6 2
- 样例输出
5
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#define MAX 501#define MAXCF 101#define min(a,b) (a)>(b)?(b):(a)using namespace std;FILE *stream;int cf[MAX][MAX];//存储图int queue[MAX];//搜索队列int path[MAX];//保存路径int capacity[MAX];//流量数组,保存经过该点的最小流量bool visited[MAX];//记录访问数组int findAugmentPath(int T){int i = 0, tail = 0;memset(visited, 0, sizeof(visited));queue[tail] = 1;//将源点加入队列capacity[1] = MAXCF;visited[1] = true;while (i <= tail){int u = queue[i];if (u == T)return capacity[T];//找到一条增广路径,返回该路径最小流量for (int v = 2; v <= T ; v++){if (!visited[v] && cf[u][v] > 0){path[v] = u;capacity[v] = min(cf[u][v], capacity[u]);//记录路径上的最小残余流量visited[v] = true;tail++;queue[tail] = v;}}i++;}return 0;}void modifyGraph(int T){int flow = capacity[T];int now = T;while (now != 1){int fa = path[now];cf[fa][now] -= flow;cf[now][fa] += flow;now = fa;}}int main(){int N, M;int i;int u, v;int temp;//freopen_s(&stream, "in.txt", "r", stdin);while (cin >> N >> M){memset(cf, 0, sizeof(cf));for (i = 0; i < M; ++i){cin >> u >> v >> temp;cf[u][v] += temp;cf[v][u] = -cf[u][v];}int maxFlow = 0;int delta = 0;while (delta = findAugmentPath(N)){maxFlow += delta;modifyGraph(N);}cout << maxFlow << endl;//fclose(stdin);//system("pause");} return 0;}
早在1955年,T.E.哈里斯就提出在一个给定的网络上寻求两点间最大运输量的问题。并且由此产生了一个新的图论模型:网络流。
用数学的语言描述就是给定一个有向图G=(V,E),其中每一条边(u,v)均有一个非负数的容量值,记为c(u,v)≥0。同时在图中有两个特殊的顶点,源点S和汇点T。
举个例子:
其中节点1为源点S,节点6为汇点T。
我们要求从源点S到汇点T的最大可行流量,这个问题也被称为最大流问题。
在这个例子中最大流量为5,分别为:1→2→4→6,流量为1;1→3→4→6,流量为2;1→3→5→6,流量为2。
输入
第1行:2个正整数N,M。2≤N≤500,1≤M≤20,000。
第2..M+1行:每行3个整数u,v,c(u,v),表示一条边(u,v)及其容量c(u,v)。1≤u,v≤N,0≤c(u,v)≤100。
给定的图中默认源点为1,汇点为N。可能有重复的边。
输出
第1行:1个整数,表示给定图G的最大流。
0 0
- 网络流一·Ford-Fulkerson算法
- hihoCoder 题目1 : 网络流一·Ford-Fulkerson算法
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder #1369 : 网络流一·Ford-Fulkerson算法
- hihocoder1369 网络流一·Ford-Fulkerson算法
- hihocoder 1369: 网络流一·Ford-Fulkerson算法
- hihocoder#1369 : 网络流一·Ford-Fulkerson算法
- hihocode#1369 : 网络流一·Ford-Fulkerson算法(EK)
- 网络流--ford-fulkerson算法
- hihoCoder-第115周-网络流一·Ford-Fulkerson算法
- 网络流Ford—Fulkerson算法
- 网络流问题Ford-Fulkerson算法详解
- 最大网络流的Ford-Fulkerson 算法
- hihoCoder 1369 网络流一·Ford-Fulkerson算法 (网络流学习#1 记录)
- 增广路算法 Ford-Fulkerson算法 网络最大流问题
- 网络流Ford-fulkerson算法及dinic算法
- 网络流 (Ford-Fulkerson)
- 最大流 Ford-Fulkerson算法
- JAVA集合-List
- 矩阵的思考-矩阵乘法
- Android中UI控件的详细介绍(五)——Button
- fragment的基本用法
- python核心编程
- 网络流一·Ford-Fulkerson算法
- diff与patch,diff的三种模式说明
- 连等初始化问题
- Android Studio 中如何引入 layoutlib.jar?
- Codevs 4909 寂寞的堆
- JAVA集合-Map
- iOS-多线程详解
- JAVA--IO流
- 第三周 项目二——建设“顺序表”算法库