POJ2914 Minimum Cut 【全局最小割】(Stoer_Wagner)
来源:互联网 发布:mac 再次登录时 编辑:程序博客网 时间:2024/05/16 03:30
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 M lines, each line contains M integersA, 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 30 1 11 2 12 0 14 30 1 11 2 12 3 18 140 1 10 2 10 3 11 2 11 3 12 3 14 5 14 6 14 7 15 6 15 7 16 7 14 0 17 3 1
Sample Output
212
Source
Wang, Ying (Originator)
Chen, Shixi (Test cases)
题意:求裸的最小割。
题解:过程有点像求“最大生成树”,每次找到将最后的点(即割点值最小的点)隔离出去的最小割大小nowcut,然后用nowcut更新结果mincut,然后将最后两个点合并,方式是将最后一个点合并到倒数第二个点,边也都合并过去,就这样循环下去直到只剩下一个顶点,此时mincut即为所求。
#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 502int map[maxn][maxn], W[maxn], hash[maxn];bool vis[maxn];void getMap(int n, int m){ int i, u, v, c; memset(map, 0, sizeof(map)); for(i = 0; i < m; ++i){ scanf("%d%d%d", &u, &v, &c); map[u][v] += c; map[v][u] += c; }}int Stoer_Wagner(int n){ int minCut = inf, nowCut, now, pre, i, j; for(i = 0; i < n; ++i) hash[i] = i; while(n > 1){ nowCut = -1; now = 1; vis[hash[0]] = 1; pre = 0; for(i = 1; i < n; ++i){ W[hash[i]] = map[hash[0]][hash[i]]; vis[hash[i]] = 0; if(W[hash[i]] > nowCut){ nowCut = W[hash[i]]; now = i; } } for(j = 1; j < n; ++j){ vis[hash[now]] = 1; if(j == n - 1){ if(nowCut < minCut) minCut = nowCut; for(i = 0; i < n; ++i){ map[hash[pre]][hash[i]] += map[hash[now]][hash[i]]; map[hash[i]][hash[pre]] += map[hash[now]][hash[i]]; } hash[now] = hash[--n]; break; } pre = now; nowCut = -1; for(i = 1; i < n; ++i){ if(!vis[hash[i]]){ W[hash[i]] += map[hash[pre]][hash[i]]; if(W[hash[i]] > nowCut){ nowCut = W[hash[i]]; now = i; } } } } } return minCut;}void solve(int n){ printf("%d\n", Stoer_Wagner(n));}int main(){ freopen("stdin.txt", "r", stdin); int n, m; while(scanf("%d%d", &n, &m) == 2){ getMap(n, m); solve(n); } return 0;}
- POJ2914 Minimum Cut 【全局最小割】(Stoer_Wagner)
- poj2914——Minimum Cut//最小割Stoer_Wagner
- 全局最小割 poj2914 Minimum Cut
- POJ2914 Minimum Cut(无向图的最小割,Stoer_Wagner算法)
- POJ2914 Minimum Cut(最小割)
- 【BZOJ3345】Minimum Cut 全局最小割 【Stoer_Wagner算法】
- POJ2914 Minimum Cut【全局最小割】【Stoer-Wangner】
- 【POJ2914】Minimum Cut-无向图的全局最小割
- POJ2914 Minimum Cut 【全局最小割Stoer-Wagner模板题】
- POJ 2914-Minimum Cut(Stoer_Wagner最小割算法)
- POJ2914 Minimum Cut 最小割集
- POJ2914 Minimum Cut(最小割模板题)
- POJ2914 Minimum Cut —— 最小割
- poj2914 全局最小割
- POJ 2914 Minimum Cut Stoer-Wagner(全局最小割)
- poj 2914 Minimum Cut(全局最小割)
- POJ-2914 Minimum Cut(全局最小割)
- POJ 2914 Minimum Cut 全局最小割
- 七天学会NodeJS
- Cocos2d-x 3.0 开发(十一)3.0 alpha1文件结构变化
- 時光
- 互联网时代的社会语言学:基于SNS的文本数据挖掘
- heap initial
- POJ2914 Minimum Cut 【全局最小割】(Stoer_Wagner)
- libgdx粒子特效 particle effect
- 转:JVM参数调优,无停滞实践
- tomcat把web项目部署到磁盘的任意目录的方法
- Binder机制1---Binder原理介绍
- ACM整理(三)——1464helloword_for_U
- Android 动态logo bootanimation.zip 制作
- 一种没有语料字典的分词方法
- Android Bundle类