stoer wagner代码
来源:互联网 发布:C语言 函数库 下载 编辑:程序博客网 时间:2024/05/29 11:39
合并的代码很容易写错!#include <cstdio>#include <string.h>#include <cstdlib>#include <algorithm>using namespace std;const int NMax=550;int N,M,G[NMax][NMax],dist[NMax],map[NMax];bool inA[NMax];int Dinic() {//1.min=MAXINT,固定一个顶点P //2.从点P用类似prim的s算法扩展出“最大生成树”,记录最后扩展的顶点和最后扩展的边 //3.计算最后扩展到的顶点的切割值(即与此顶点相连的所有边权和),若比min小更新min //4.合并最后扩展的那条边的两个端点为一个顶点(当然他们的边也要合并,这个好理解吧?) //5.转到2,合并N-1次后结束 //6.min即为所求,输出min int ret=(~0u>>1),s=1,pre;for(int i=1;i<=N;i++) map[i]=i;while(N>1) {memset(inA,0,sizeof(inA)); inA[map[s]]=1;int lastx,lasty;pre=s;for(int i=1;i<=N;i++) dist[map[i]]=G[map[s]][map[i]];for(int I=1;I<N;I++) {int next=-1;for(int i=2;i<=N;i++) if(!inA[map[i]]&&(next==-1||dist[map[i]]>dist[map[next]])) next=i;inA[map[next]]=1;for(int i=1;i<=N;i++) if(!inA[map[i]]) dist[map[i]]+=G[map[next]][map[i]];if(I==N-1) {lastx=pre;lasty=next;}pre=next;}ret=min(ret,dist[map[lasty]]);for(int i=1;i<=N;i++) G[map[lastx]][map[i]]=G[map[i]][map[lastx]]=G[map[lastx]][map[i]]+G[map[i]][map[lasty]];//就是上一行,千万不要写成G[map[lastx]][map[lasty]]+G[map[i]][map[lasty]]map[lasty]=map[N--];}return ret==(~0u>>1)?0:ret;}int main(){while(scanf("%d%d",&N,&M)!=EOF) {int x,y,z; memset(G,0,sizeof(G));for(int i=1;i<=M;i++) {scanf("%d%d%d",&x,&y,&z);x++;y++;G[x][y]+=z;G[y][x]+=z;}printf("%d\n",Dinic());}return 0;}
- stoer wagner代码
- poj2914 stoer wagner算法
- CUT Stoer-Wagner Algorithm [转]
- 最小割 Stoer-Wagner 算法
- 【最小割】Stoer-Wagner模板
- 最小割集Stoer-Wagner算法【ZZ】
- 最小割集Stoer-Wagner算法
- 最小割集Stoer-Wagner算法
- 最小割集Stoer-Wagner算法
- Stoer-Wagner算法讲解(图+转载)
- 最小割集Stoer-Wagner算法
- 最小割Stoer-Wagner算法模板hdu3691
- Stoer-Wagner算法求全局最小割
- poj2914-Minimum Cut-Stoer-Wagner算法
- poj 2914 最小割 Stoer-Wagner 算法
- 全局最小割集Stoer-Wagner算法
- Stoer-Wagner算法(最小割集)
- 最小割集Stoer-Wagner算法,网络最大流问题
- Linux/Unix下去除window下换行符^M
- 找出文件的所有符号链接
- easyui入门(一)
- Spring中ClassPathXmlApplicationContext类的简单使用
- 蛇形填数
- stoer wagner代码
- NYOJ-102:次方求模
- 使用一组radio button
- 九度1077 最大序列和
- 多线程编程模型
- Adobe AIR教程:iOS应用内付费简介和准备流程
- 整理综合之一 ----从Bytecode到BasicBlock
- Android之ExpandableListView
- 设计模式(十六)----- 迭代器模式(Iterator)----(JAVA版)