Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 9319   Accepted: 3910
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 ≤ M ≤ N × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are Mlines, each line contains M integers AB and C (0 ≤ AB < NA ≠ BC > 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)

分析:

这道题也需要枚举...不过和求点连通度相比较,求边连通度的时候只需要任意选取源点,然后枚举汇点即可...

为何?

因为最小割可以把原图分成ST两个集合...我们选取了一个源点u,一定存在另一个点存在于另一个集合...

但是求点连通度的时候我们可能选取的两个点刚好是需要割的点...所以我们必须枚举所有的点对...

但是这是暴力的方法...还有另一种复杂度要低的做法--stoer_wagner

其算法的精髓在于不断合并点对信息从而缩小图的规模...

我们考虑最小割可以把原图分为两个集合ST...我们选取了st两个点,如果st位于一个集合,那么我们把st合并成一个点对答案是没有影响的...如果位于两个集合,那么求出的最小割就是答案...

所以我们先随便选取一个点扔入A集合,然从后当前点开始延伸,更新与当前点有边相连的点的权值(权值就是加上边权)...然后选取一个权值最大的点扔入A集合,继续更新...

然后到当只剩下最后一个点没有并入A集合的时候,我们就把这个点选为t点,把倒数第二个并入A集合的点选为s点,t的权值就是当前的最小割...然后合并st...再在新图上进行选点求最小割的操作...

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
#define inf 0x3f3f3f3f
using namespace std;
//大鹏一日同风起,扶摇直上九万里 const int maxn=+; int n,m,w[maxn],mp[maxn][maxn],bel[maxn],vis[maxn]; inline int stoer_wagner(void){
int ans=inf;
for(int i=;i<=n;i++)
bel[i]=i;
while(n>){
memset(w,,sizeof(w));
memset(vis,,sizeof(vis));
int pre=;vis[bel[pre]]=;
for(int i=;i<=n-;i++){
int k=-;
for(int j=;j<=n;j++)
if(!vis[bel[j]]){
w[bel[j]]+=mp[bel[pre]][bel[j]];
if(k==-||w[bel[k]]<w[bel[j]])
k=j;
}
vis[bel[k]]=;
if(i==n-){
int S=bel[pre],T=bel[k];
ans=min(ans,w[T]);
for(int j=;j<=n;j++)
mp[S][bel[j]]+=mp[bel[j]][T],mp[bel[j]][S]+=mp[bel[j]][T];
bel[k]=bel[n];n--;
}
pre=k;
}
}
return ans==inf?:ans;
} signed main(void){
while(scanf("%d%d",&n,&m)!=EOF){
memset(mp,,sizeof(mp));
for(int i=,s,x,y;i<=m;i++)
scanf("%d%d%d",&x,&y,&s),x++,y++,mp[x][y]+=s,mp[y][x]+=s;
printf("%d\n",stoer_wagner());
}
return ;
}//Cap ou pas cap. Cap.

By NeighThorn

POJ 2914 Minimum Cut的更多相关文章

  1. POJ 2914 Minimum Cut 最小割图论

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

  2. POJ 2914 Minimum Cut Stoer Wagner 算法 无向图最小割

    POJ 2914 题意:给定一个无向图 小于500节点,和边的权值,求最小的代价将图拆为两个联通分量. Stoer Wagner算法: (1)用类似prim算法的方法求"最大生成树" ...

  3. POJ 2914 - Minimum Cut - [stoer-wagner算法讲解/模板]

    首先是当年stoer和wagner两位大佬发表的关于这个算法的论文:A Simple Min-Cut Algorithm 直接上算法部分: 分割线 begin 在这整篇论文中,我们假设一个普通无向图G ...

  4. POJ 2914 Minimum Cut (全局最小割)

    [题目链接] http://poj.org/problem?id=2914 [题目大意] 求出一个最小边割集,使得图不连通 [题解] 利用stoerwagner算法直接求出全局最小割,即答案. [代码 ...

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

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

  6. POJ 2914 Minimum Cut 全局最小割

    裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...

  7. POJ 2914 Minimum Cut 最小割算法题解

    最标准的最小割算法应用题目. 核心思想就是缩边:先缩小最大的边.然后缩小次大的边.依此缩小 基础算法:Prime最小生成树算法 只是本题測试的数据好像怪怪的,相同的算法时间执行会区别非常大,并且一样的 ...

  8. POJ2914 Minimum Cut —— 最小割

    题目链接:http://poj.org/problem?id=2914 Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Sub ...

  9. POJ Minimum Cut

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3902 Case ...

随机推荐

  1. 25M电子琴实现

    module qin(input clk,output reg beep,input [3:0] col,output [3:0] row_data,output [7:0]out ,input rs ...

  2. PostgreSQL保存文件到数据库

    1.CREATE TABLE public.t_file ( id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('t_file_id_seq'::regc ...

  3. 1、ASP.NET MVC入门到精通——新语法

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 在学习ASP.NET MVC之前,有必要先了解一下C#3.0所带来的新的语法特性,这一点尤为重要,因为在MVC项目中我们利用C#3.0的新特 ...

  4. mariadb 10.2.3支持延时复制

    在mysql 5.6开始就支持延时复制,这在一些需要维护大量非标准化系统或者运维技术水平较低的公司和开发人员众多的项目组这是一个非常有价值的特性,可以说误操作的概率跟一个城市车祸概率的水平差不多了,我 ...

  5. S1的小成果:MyKTV系统

    转眼之间,已经到了2016年,即新的一年了!S1也结束了,收获的也不多 ,想想最后留给大家的就一个KTV项目了. 希望大家看时有所收获           现在我们一起来看KTV前台管理 主界面的运行 ...

  6. 原生HTML5 input type=file按钮UI自定义

    原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...

  7. js 数组去重(7种)

    第一次写技术博客,之前总是认为写这些会很浪费时间,还不如多看几篇技术博文.但...但昨天不知道受了什么刺激,好像有什么在驱使着自己要写一样,所以才有了今天的第一篇博文.总觉得应该要坚持这样写下去.初次 ...

  8. 码农干货系列【20】--add gtTime to Promise.js

    使用场景 在一些时候,希望一件task不能太快完成,需要大于多少时间才可以执行,就可以使用Promise的gtTime方法. 使用方式 Promise.gtTime(f1(), 5000).then( ...

  9. 第一个随笔,调试中,用的CSS3

    希望能在博客园很好的学习并得到技术上的提升!

  10. IE6/IE7中li底部4px空隙的Bug

    当li的子元素中有浮动(float)时,IE6/IE7中<li>元素的下面会产生4px空隙的bug. 代码如下: <ul class="list"> < ...