最大流算法_一般曾广路算法Ford
2015-08-27 22:23
316 查看
/* 最大流增广路算法 */ #include<iostream> #include<queue> #include<cstdio> #include<cstring> #include<cmath> #define oo 0x3f3f3f3f const int maxn = 1010; using namespace std; struct point { int c,f; }; point mp[maxn][maxn]; int n,m; int flag[maxn]; int par[maxn]; int add[maxn]; int que[maxn],qe,qs; void Ford(int S, int T) { while(1) { memset(flag, -1,sizeof(flag)); memset(par, 0,sizeof(par)); memset(add,0,sizeof(add)); flag[S] = 0; par[S] = 0; add[S] = oo; qe = qs = 0; que[qs++] = S; while(qe<qs&&flag[T]==-1) { int u = que[qe++]; for(int i = 1; i <= n; i++) { if(flag[i]==-1)//未标记 { if(mp[u][i].c != oo&&mp[u][i].c-mp[u][i].f>0)//正向 { flag[i] = 0; par[i] = u; add[i] = min(add[u],mp[u][i].c-mp[u][i].f); que[qs++] = i; } else if(mp[i][u].c != oo && mp[i][u].f>0)//反向 { flag[i] = 0; par[i] = -u; add[i] = min(add[u], mp[i][u].f); que[qs++] = i; } } } flag[u]=1; } if(flag[T]==-1 || add[T]==0)break; int k1 = T, k2 = abs(par[k1]); int a = add[T]; while(1) { if(mp[k2][k1].c != oo) { mp[k2][k1].f += a; } else mp[k1][k2].f -= a; if(k2==S)break; k1 = k2; k2 = abs(par[k1]); } }***重点内容*** int maxFlow = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(i == S&&mp[i][j].f<oo) maxFlow += mp[i][j].f; if(mp[i][j].f<oo) printf("%d-->%d:%d\n",i,j,mp[i][j].f); } } printf("最大流:%d\n",maxFlow); } int main() { int u,v,c,f; scanf("%d%d",&n,&m); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) mp[i][j].c = mp[i][j].f = oo; } for(int i = 0; i < m; i++) { scanf("%d%d%d%d",&u,&v,&c,&f); mp[u][v].c = c; mp[u][v].f = f; } Ford(1,n); } /* 6 10 1 2 4 3 1 3 8 2 2 3 4 2 2 4 4 0 2 5 1 1 3 4 2 2 3 5 2 2 4 6 7 2 5 4 6 0 5 6 9 3 6 10 1 2 8 2 1 3 4 3 2 4 2 2 2 5 2 2 3 2 4 2 3 4 1 1 3 5 4 0 4 5 6 0 4 6 9 3 5 6 7 2 */
相关文章推荐
- 一些loadrunner自学以及问题解答
- 最新版本VLC2.2.1编译
- Simplify Path
- iOS中的UILabel
- UISegmentedControl
- 栈的压入、弹出序列
- java环境变量配置
- Introduction to Java Programming编程题9.12<变位词>
- python 字符串组成MySql 命令时,字符串含有单引号或者双引号导致出错解决办法
- HDU 1863 畅通工程
- 如何使LoadRunner实现加密测试
- Roman to Integer
- nmon下载安装
- STM32 Timer : Base Timer, Input Capture, PWM, Output Compare
- iOS中的按钮(UIButton)
- storm wordcount实例
- HDU 1423
- 有关 MySQL数据库常见的误解
- 多线程之volatile
- Integer to Roman