MaxFlow(最大流增广路算法)
2011-05-30 19:58
316 查看
#include<iostream> #include<queue> #include<fstream> using namespace std; const int MAXN = 201; int cap[MAXN][MAXN]; //残留网络,初始为原图 int flow[MAXN][MAXN]; int vernum; //节点总数量 int pre[MAXN]; int BFS(const int &s, const int &t) { queue<int>q; memset(pre,-1,sizeof(pre)); q.push(s); pre[s] = s; while (!q.empty()) { int u = q.front(); q.pop(); for (int v=0;v<vernum;v++) { if( (pre[v]==-1)&&(flow[u][v]<cap[u][v] || flow[v][u]>0) ) { pre[v] = u; q.push(v); if (v == t) return 1; } } } return 0; } void TrackBack(const int &s, const int &t) { int v,min = 0x7fffffff; int i=t; while (i != s) { v = pre[i]; if (flow[v][i] < cap[v][i]) { if(min > cap[v][i] - flow[v][i]) min = cap[v][i] - flow[v][i]; } else { if (flow[i][v] > 0) { if(min > flow[i][v]) min = flow[i][v]; } } i = v; } i = t; while (i != s) { v = pre[i]; if (flow[v][i] < cap[v][i])//前进流+min flow[v][i] += min; else { if (flow[i][v] > 0) flow[i][v] -= min; } i = v; } } int MaxFlow(const int &s, const int &t) { while (BFS(s, t)) TrackBack(s, t); int res = 0; for(int i=0;i<vernum;i++) { if (i != s) res += flow[s][i]; } return res; } int main() { ifstream cin("in.txt"); ofstream cout("out.txt"); int arcnum,f,t,w; while(cin >> vernum >> arcnum) { memset(cap,0,sizeof(cap)); memset(flow,0,sizeof(flow)); while (arcnum--) { cin >> f >> t >> w; cap[f-1][t-1] += w; } int ans = MaxFlow(0,vernum-1); cout << ans << endl; } return 0; }
相关文章推荐
- 最大流增广路算法
- 图论最大网络流增广路算法详解与实现
- 匈牙利算法求二分图的最大匹配寻找增广路的几种思路(转)
- 最大网络流——增广路算法
- POJ1273 Drainage Ditches【最大流、增广路算法Edmonds_Karp】
- 最大流的增广路算法(EK)
- 最大流 增广路算法+最小费用最大流
- poj1459Power Network_最大流的增广路算法_多源点多汇点问题(Edmord_Karp)
- poj 3436 最大流的增广路算法
- 网络流初步 增广路算法求最大流 hdoj3549
- [最大流]增广路算法Edmonds-Karp
- 网络最大流中一般增广路算法(标号法)
- 最大流ISAP(距离标号最短增广路算法)模板
- Power Network (最大流增广路算法模板题)
- 网络最大流求解 增广路算法
- HDU 3549 Flow Problem 最大流 最小增广路 EK算法 传说中的入门算法
- 最大流的增广路算法比较
- PIGS(最大流_增广路算法)
- poj 1459 Power Network 初级->图算法->最大流(基本算法:增广路)
- 最大流 - Edmonds-Karp 增广路算法