POJ 2914 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 A, B and C (0 ≤ A, B < N, A ≠ B, 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
分析:
这道题也需要枚举...不过和求点连通度相比较,求边连通度的时候只需要任意选取源点,然后枚举汇点即可...
为何?
因为最小割可以把原图分成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的更多相关文章
- POJ 2914 Minimum Cut 最小割图论
Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...
- POJ 2914 Minimum Cut Stoer Wagner 算法 无向图最小割
POJ 2914 题意:给定一个无向图 小于500节点,和边的权值,求最小的代价将图拆为两个联通分量. Stoer Wagner算法: (1)用类似prim算法的方法求"最大生成树" ...
- POJ 2914 - Minimum Cut - [stoer-wagner算法讲解/模板]
首先是当年stoer和wagner两位大佬发表的关于这个算法的论文:A Simple Min-Cut Algorithm 直接上算法部分: 分割线 begin 在这整篇论文中,我们假设一个普通无向图G ...
- POJ 2914 Minimum Cut (全局最小割)
[题目链接] http://poj.org/problem?id=2914 [题目大意] 求出一个最小边割集,使得图不连通 [题解] 利用stoerwagner算法直接求出全局最小割,即答案. [代码 ...
- POJ 2914 Minimum Cut【最小割 Stoer-Wangner】
题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...
- POJ 2914 Minimum Cut 全局最小割
裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...
- POJ 2914 Minimum Cut 最小割算法题解
最标准的最小割算法应用题目. 核心思想就是缩边:先缩小最大的边.然后缩小次大的边.依此缩小 基础算法:Prime最小生成树算法 只是本题測试的数据好像怪怪的,相同的算法时间执行会区别非常大,并且一样的 ...
- POJ2914 Minimum Cut —— 最小割
题目链接:http://poj.org/problem?id=2914 Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Sub ...
- POJ Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3902 Case ...
随机推荐
- JS效果集锦
秒后消失 $('.success_message').text( '反馈成功' ); setTimeout(function(){ $( ".success_messa ...
- 关系型数据库与NOSQL
本文转载自: http://www.cnblogs.com/chay1227/archive/2013/03/17/2964020.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) ...
- CSS实现弹出导航菜单
查看实际效果:http://keleyi.com/a/bjac/vksd7321.htm 完整代码,保存在html文件打开也可看到效果: <!DOCTYPE html PUBLIC " ...
- jquery叠加页片自动切换特效
查看效果:http://keleyi.com/keleyi/phtml/jqtexiao/34.htm 下面是HTML代码: <!DOCTYPE html> <html xmlns= ...
- 二次、三次贝塞尔曲线demo(演示+获取坐标点)
二次贝塞尔曲线demo: See the Pen quadraticCurveDemo by hanyanjun (@hanyanjun) on CodePen. 我的demo地址(二次) 推荐点击以 ...
- 关于SharePoint 2013的工作流(一)
从去年开始,一直和SharePoint 2013工作流打交道.自己瞎摸索,以实现功能为目的.直到如今也不知道走的路是否正确. 一开始用WF4发现整个都不一样了,用的xaml无法写后端代码.Google ...
- 两种方法设置disabled属性
//两种方法设置disabled属性 $('#fileup').attr("disabled",true); $('#fileup').attr("disabled&qu ...
- AMD规范与CMD规范的区别是什么?
AMD规范与CMD规范的区别是什么? 在比较之前,我们得先来了解下什么是AMD规范?什么是CMD规范?当然先申明一下,我个人也是总结下而已,也是网上看到的资料,自己总结下或者可以说整理下而已,供 ...
- IOS开发基础知识--碎片25
1:使用@protocol实现delegate和datasource模式 #import <UIKit/UIKit.h> @protocol MyViewDataSource,MyView ...
- iOS 疑难杂症— — 收到推送显示后自动消失的问题
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 问题 正在支持 Remote Noti ...