描述


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. asp:时间的计算

    DataTime dt = new DataTime();//dt为时间DataTime对象 dt.ToString();//2005-11-5 13:47:04 dt.AddYears(1).ToS ...

  2. rapidxml对unicode的支持

    为了提高duilib创建布局控件的效率,在LuaDui项目中使用rapidxml解析器替换了duilib库自带的xml解析器. duilib使用unicode编译,所以rapidxml需要解析unic ...

  3. (转)C++设计模式——观察者模式

    转自:http://www.jellythink.com/archives/359 前言 之前做了一个性能测试的项目,就是需要对现在的产品进行性能测试,获得测试数据,然后书写测试报告,并提出合理化的改 ...

  4. mybatis()

    ---------------------------------mysql分页---------------------------------- public void selectList(in ...

  5. nginx 自定义代理返回 404

    在nginx的http段,加上一面的配置 proxy_intercept_errors on;//自定义代理返回的404错误提示

  6. CentOS系统Apache服务器优化详解

    1.Apache优化 Apache能够在CentOS系统正常运行.但是,对于访问量稍大的站点,Apache的这些默认配置是无法满足需求的,我们仍需调整Apache的一些参数,使Apache能够在大访问 ...

  7. 一个开源的可视化的jQuery工作流插件

    特点 1.跨浏览器,可兼容IE7--IE11, FireFox, Chrome, Opera等几大内核的浏览器,且不需要浏览器再加装任何控件. (IE7-IE8时,使用VML:IE9以上,FF,OPE ...

  8. 基于ECharts 的地图例子

      最近的一个项目要用到显示地图,本来用jq做了一个,但由于客户不满意(确实自己弄的样式效果都不是太理想),于是就上网搜了搜,最后决定基于百度的ECharts来弄地图 本来自己js基础不是很扎实,EC ...

  9. apache2.2+PHP5.4.28

    搭建apache+php开发环境,apache一路正常安装,但是,下载的php搭建后,配置好apache.php,始终报错“The requested operation has failed!”换了 ...

  10. css(html)背景图优化合并

    图片本身的优化: 图像质量要求和图像文件大小决定你用什么格式的图片,用较小的图片文件呈现较好的图像质量. 当图片色彩过于丰富且无透明要求时,建议采用jpg格式并保存为较高质量. 当图片色彩过于丰富又有 ...