POJ Minimum Cut
2016-12-28 18:47
281 查看
Minimum Cut
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 M lines, each line contains Mintegers 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 Baidu Star 2006 Semifinal Wang, Ying (Originator) Chen, Shixi (Test cases) |
无向图的边连通度,有一个神奇的算法,还有我蒟蒻的模板。
#include <cstdio> #include <cstring> inline int min(int a, int b) { return a < b ? a : b; } const int inf = 2e9; const int maxn = 505; int n, m; int vis[maxn]; int wet[maxn]; int com[maxn]; int G[maxn][maxn]; inline int search(int &S, int &T) { memset(vis, 0, sizeof(vis)); memset(wet, 0, sizeof(wet)); S = -1, T = -1; int id, maxi, ret = 0; for (int i = 0; i < n; ++i) { maxi = -inf; for (int j = 0; j < n; ++j) if (!com[j] && !vis[j] && wet[j] > maxi) id = j, maxi = wet[j]; if (id == T) return ret; S = T; T = id; ret = maxi; vis[id] = 1; for (int j = 0; j < n; ++j) if (!com[j] && !vis[j]) wet[j] += G[id][j]; } } inline int StoerWagner(void) { int ret = inf, S, T; memset(com, 0, sizeof(com)); for (int i = 0; i < n - 1; ++i) { ret = min(ret, search(S, T)); if (!ret)return 0; com[T] = 1; for (int j = 0; j < n; ++j) if (!com[j]) G[S][j] += G[T][j], G[j][S] += G[j][T]; } return ret; } signed main(void) { while (~scanf("%d%d", &n, &m)) { memset(G, 0, sizeof(G)); for (int i = 1; i <= m; ++i) { int x, y, w; scanf("%d%d%d", &x, &y, &w); G[x][y] += w; G[y][x] += w; } printf("%d\n", StoerWagner()); } }
@Author: YouSiki
相关文章推荐
- java 基础
- 如果滚动,就将搜索框浮动到最顶部(支持浏览器兼容)
- 内核链接脚本分析
- JS引用传递和值传递
- linux ubuntu 搭建
- make 模板
- jQuery第一篇 (帅哥)
- 大数据学习笔记之五 数据中心的三层结构
- 项目中生成下载目录
- 机器人自主定位导航 ROS真的好用吗?优缺点剖析
- 机器人自主定位导航 ROS真的好用吗?优缺点剖析
- ABP文档翻译--值对象
- Windows自带压缩解压工具
- PHP安装mysql.so扩展
- CocoaPods 导入第三方库头文件自动补齐
- linux初学<习题二>管理刚才新建立的文件!
- 判断是什么浏览器
- PhxSQL使用及部署——替换druid metadata使用的mysql
- eclipse、myeclipse 反编译插件 轻松查看源代码
- JavaIO 文件递归遍历及常用File类操作