描述


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. 获取汉字拼音 Java

    两种方法:一个是使用btye数组,一个是引入jar包进行操作. 1. public class CharacterParser { private static int[] pyvalue = new ...

  2. input设置disabled,经过strus2提交到后台,后台取不到值

    页面中有多个name相同的input与后台action中一个属性对应,一直在好奇为什么会可以提交到后台呐,但是有时还报这个属性找不到对应的方法(多个name相同好像匹配的是数组,所以找不到), 但是我 ...

  3. oracle job interval·相关事例

    描述 Interval参数值 每天运行一次 'SYSDATE + 1' 每小时运行一次 'SYSDATE + 1/24' 每10分钟运行一次 'SYSDATE + 10/(60*24)' 每30秒运行 ...

  4. asp.net:用类来后台绑定数据源

    //封装成一个 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Syste ...

  5. 查看编译后的calss文件编译jdk版本

    使用UtralEdit或者sublime text打开编译后的.class文件, 其中cafe babe为magic number(魔数),标识这个文件是java的class文件. 0033转换成10 ...

  6. rpm命令详解

    http://www.rpm.org/max-rpm/s1-rpm-install-additional-options.html#S2-RPM-INSTALL-REPLACEFILES-OPTION ...

  7. Jquery 实现瀑布流布局

    //保证img文件夹下有图片//引入jquery <script src="Script/jquery-1.7.2.js"></script> <st ...

  8. HTML块

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. mysqli和mysql和pdo查询

      mysql mysql_connect($db_host, $db_user, $db_password); mysql_select_db($dn_name); $result = mysql_ ...

  10. php获取文件mime类型Fileinfo等方法

    前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...