描述


http://poj.org/problem?id=2914

求无向图中最小割.

Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 8679   Accepted: 3659
Case Time Limit: 5000MS

Description

Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs?

Input

Input contains multiple test cases. Each test case starts with two integers N and M (2 ≤ N ≤ 500, 0 ≤ MN × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are M lines, each line contains M integers A, B and C (0 ≤ A, B < N, AB, C > 0), meaning that there C edges connecting vertices A and B.

Output

There is only one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0.

Sample Input

3 3
0 1 1
1 2 1
2 0 1
4 3
0 1 1
1 2 1
2 3 1
8 14
0 1 1
0 2 1
0 3 1
1 2 1
1 3 1
2 3 1
4 5 1
4 6 1
4 7 1
5 6 1
5 7 1
6 7 1
4 0 1
7 3 1

Sample Output

2
1
2

Source

Baidu Star 2006 Semifinal
Wang, Ying (Originator)

Chen, Shixi (Test cases)

分析


不会做啊...

可以暴力枚举源点和汇点,然后开始瞎搞...必定超时啊...

有专门解决这种问题的算法: Stoer_Wagner.

好吧其实我并没有理解是为啥......感觉只知道算法思路.

设所要求的最小割为Cut.先找任意s,t的最小割,如果s,t在Cut两侧,则割(s,t)就是Cut,否则割(s,t)>=Cut,并且将s,t合成一个点不会影响Cut.就这样,我们每次找任意的s,t的割,然后合并.在找到分居Cut两侧的s,t之前,合并对结果没有影响,也就是说Cut还在图中.当某一步找到s,t分居在Cut两侧的时候,那一步的割(s,t)就是Cut,如果直到最后一步前还没有出现这种情况,最后一步只有两个点,只有一个割,又因为Cut一定在图中,所以图中的割就是Cut,综上,一定能找到Cut.但是我们不知道是哪一步找到的,所以记录一个min值就好了.

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define for2(i,a,n) for(int i=(a);i<n;i++)
#define read(a) a=getnum()
#define CC(i,a) memset(i,a,sizeof(i))
using namespace std; const int maxn=+,INF=0x7fffffff;
int n,m;
int v[maxn],w[maxn];
bool vis[maxn];
int g[maxn][maxn]; inline int getnum() { int r=;char c;c=getchar();while(c<''||c>'') c=getchar();while(c>=''&&c<='') {r=r*+c-'';c=getchar();}return r; } int stoer_wagner(int n)
{
int min_cut=INF;
for1(i,,n) v[i]=i;
while(n>)
{
int pre=;
CC(vis,);
CC(w,);
for2(i,,n)
{
int k=-;
for1(j,,n)
{
if(!vis[v[j]])
{
w[v[j]]+=g[v[j]][v[pre]];
if(k==-||w[v[j]]>w[v[k]])
{
k=j;
}
}
}
vis[v[k]]=true; if(i==n-)
{
const int s=v[pre],t=v[k];
min_cut=min(min_cut,w[t]);
for1(j,,n)
{
g[s][v[j]]+=g[t][v[j]];
g[v[j]][s]+=g[t][v[j]];
}
v[k]=v[n--];
}
pre=k;
}
}
return min_cut;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("min.in","r",stdin);
freopen("min.out","w",stdout);
#endif
while(scanf("%d%d",&n,&m)!=EOF)
{
CC(g,);
while(m--)
{
int u,v,w;
read(u); read(v); read(w);
u++; v++;
g[u][v]+=w;
g[v][u]+=w;
}
printf("%d\n",stoer_wagner(n));
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("min.out");
#endif
return ;
}

POJ_2914_Minimum_Cut_(Stoer_Wagner)的更多相关文章

  1. POJ 2914 Minimum Cut

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 9319   Accepted: 3910 Case ...

  2. POJ2914 (未解决)无向图最小割|Stoer-Wagner算法|模板

    还不是很懂,贴两篇学习的博客: http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html http://blog.sina.c ...

  3. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  4. POJ2914

    POJ2914 无向图的最小割 题意:给你一个无向图,然后去掉其中的n条边,使之形成两个连通分量,也即原无向图不连通,求n的最小值. 输入: m(无向图点集),n(无向图边集) a,b,c(a,b两点 ...

  5. POJ 2914 Minimum Cut 最小割图论

    Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...

  6. poj Minimum( CutStoer Wagner算法)

    Minimum Cut 题目: 给出一张图.要求你删除最小割权和图. 算法分析: ////////////////////     转载 --- ylfdrib   ///////////////// ...

  7. POJ 2914 Minimum Cut【最小割 Stoer-Wangner】

    题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...

  8. 无向图最小割Stoer-Wagner算法学习

    无向连通网络,去掉一个边集可以使其变成两个连通分量则这个边集就是割集,最小割集当然就权和最小的割集. 使用最小切割最大流定理: 1.min=MAXINT,确定一个源点 2.枚举汇点 3.计算最大流,并 ...

  9. HDU 6081 度度熊的王国战略(全局最小割堆优化)

    Problem Description度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族.哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士.所以这一场战争,将会十分艰难.为了更好的进攻 ...

随机推荐

  1. 【POJ2094】【差分序列】Angry Teacher

    Description Mr. O'Cruel is teaching Math to ninth grade students. Students of course are very lazy, ...

  2. MongoDB笔记(二)访问权限

    要访问数据库,那么对访问权限的设置是必须的! 1.启用权限控制(-auth),当启用MongoDB数据库服务时,对参数的设置可以决定是否启用权限控制   不开启: mongod -dbpath=D:/ ...

  3. 基于NodeJs的网页爬虫的构建(二)

    好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...

  4. 数据结构学习——shell排序的C语言实现

    shell排序: 这个排序的命名是来自发明者的名字,和排序的方法没有字面上的联系.所以不要因为名字而感觉很难.在K&R的C程序设计语言中书中只用了几行代码很简洁的实现了这个排序算法.那就来看看 ...

  5. 采用python获得并修改文件编码(原创)

    windows和linux采用了不同的编码,这让很多人伤透了脑经,这里我采用了Python的chardet库获得代码的编码,然后修改编码. 1.首先需要安装chardet库,有很多方式,我才用的是比较 ...

  6. php Static静态关键字

    静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { private static $speed =  ...

  7. PHP实现递归的三种方法

    递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...

  8. linux mysql字符编码问题

    发布:thatboy   来源:脚本学堂     [大 中 小] 本文介绍下,linux环境中mysql字符编码问题的解决办法,有遇到mysql编码问题的朋友,可以参考下本文的介绍,希望对你有一定的帮 ...

  9. php中将地址生成迅雷快车旋风链接的代码

    function zhuanhuan() { $urlodd=explode('//',$_GET["url"],2);//把链接分成2段,//前面是第一段,后面的是第二段 $he ...

  10. (转载)将DELPHI数据库连接写进INI配置文件中

    将DELPHI数据库连接写进INI配置文件中 procedure TDM.DataModuleCreate(Sender: TObject); var piececonfg:Tinifile; pat ...