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

  1. 3 3
  2. 0 1 1
  3. 1 2 1
  4. 2 0 1
  5. 4 3
  6. 0 1 1
  7. 1 2 1
  8. 2 3 1
  9. 8 14
  10. 0 1 1
  11. 0 2 1
  12. 0 3 1
  13. 1 2 1
  14. 1 3 1
  15. 2 3 1
  16. 4 5 1
  17. 4 6 1
  18. 4 7 1
  19. 5 6 1
  20. 5 7 1
  21. 6 7 1
  22. 4 0 1
  23. 7 3 1

Sample Output

  1. 2
  2. 1
  3. 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...再在新图上进行选点求最小割的操作...

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. //by NeighThorn
  6. #define inf 0x3f3f3f3f
  7. using namespace std;
  8. //大鹏一日同风起,扶摇直上九万里
  9.  
  10. const int maxn=+;
  11.  
  12. int n,m,w[maxn],mp[maxn][maxn],bel[maxn],vis[maxn];
  13.  
  14. inline int stoer_wagner(void){
  15. int ans=inf;
  16. for(int i=;i<=n;i++)
  17. bel[i]=i;
  18. while(n>){
  19. memset(w,,sizeof(w));
  20. memset(vis,,sizeof(vis));
  21. int pre=;vis[bel[pre]]=;
  22. for(int i=;i<=n-;i++){
  23. int k=-;
  24. for(int j=;j<=n;j++)
  25. if(!vis[bel[j]]){
  26. w[bel[j]]+=mp[bel[pre]][bel[j]];
  27. if(k==-||w[bel[k]]<w[bel[j]])
  28. k=j;
  29. }
  30. vis[bel[k]]=;
  31. if(i==n-){
  32. int S=bel[pre],T=bel[k];
  33. ans=min(ans,w[T]);
  34. for(int j=;j<=n;j++)
  35. mp[S][bel[j]]+=mp[bel[j]][T],mp[bel[j]][S]+=mp[bel[j]][T];
  36. bel[k]=bel[n];n--;
  37. }
  38. pre=k;
  39. }
  40. }
  41. return ans==inf?:ans;
  42. }
  43.  
  44. signed main(void){
  45. while(scanf("%d%d",&n,&m)!=EOF){
  46. memset(mp,,sizeof(mp));
  47. for(int i=,s,x,y;i<=m;i++)
  48. scanf("%d%d%d",&x,&y,&s),x++,y++,mp[x][y]+=s,mp[y][x]+=s;
  49. printf("%d\n",stoer_wagner());
  50. }
  51. return ;
  52. }//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. git 常用指令

    下载项目 git clone https://git.oschina.net/jianqingwang/jianblog.git 注意,clone跟的是项目地址 查看分支(也就是版本) git bra ...

  2. CSS:CSS使用Tips

    Css是前端开发中效果展现的主要部分之一,良好的Css书写习惯可以为实际的项目开发提高效率,也可以为实现良好的团队合作提供保证. 一般新手在使用Css的时候经常会犯一些错误,出现一些不经意的漏洞,如果 ...

  3. Javascript 创建对象方法的总结

    最近看了一下<Javascript高级程序设计(第三版)>,这本书很多人都推荐,我也再次郑重推荐一下.看过之后总得总结一下吧,于是我选了这么一个主题分享给大家. 使用Javascript创 ...

  4. jQuery基础知识总结

    1.  jQuery基本概念介绍             1.1 什么是jQuery 一个javascript库,把常用方法写到一个js文件中,需要的时候直接调用即可 学习jQuery就是学习一些方法 ...

  5. 熟悉vs2012IDE

    使用vs2012已经几个月了,深感对开发环境的学习有助于提高开发的效率.现将我的经验总结如下: 一.搜索 vs2012相比vs2010添加了正则搜索,极大的提高了代码的查询效率. 二.重构 同vs20 ...

  6. 【超全整理】J2EE集成开发环境MyEclipse使用心得汇总

    一.首先我们为什么需要MyEclipse? 下面允许我做一些简要的介绍: 应该大家都知道另一个MyEclipse的近亲——Eclipse的优点:免费.程序代码排版功能.有中文汉化包.可增设许多功能强大 ...

  7. Sql 获取日期区间

    获取制定日期区间 declare @d as date declare @d2 as date set @d = '2014-06-03' set @d2 ='2014-06-10' ),datead ...

  8. iOS之UI--富文本总结

    文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 ...

  9. Solr实现Low Level查询解析(QParser)

    Solr实现Low Level查询解析(QParser) Solr基于Lucene提供了方便的查询解析和搜索服务器的功能,可以以插件的方式集成,非常容易的扩展我们自己需要的查询解析方式.其中,Solr ...

  10. 浅谈UIAlertController使用

    一开始在刚接触到Alert和ActionSheet的时候,经常傻傻分不清楚,好不容易用习惯了,苹果又给合并了,好在用起来也不困难,到底哪个好呢?见仁见智吧! 现在稍微介绍一下怎么用. 1.初始化,一般 ...