POJ 2914 无向图的最小割,模板题。
来源:互联网 发布:淘宝运营高级教程视频 编辑:程序博客网 时间:2024/05/17 22:17
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, whereN is the number of vertices. Following are M lines, each line containsM integers A, B and C (0 ≤ A, B <N, A ≠ B, C > 0), meaning that there C edges connecting verticesA 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)
#include <iostream>#include <cstdio>#include <cstring>#define MAXN 505#define INF 1000000000using namespace std;int map[MAXN][MAXN];int v[MAXN], dis[MAXN]; //v数组是马甲数组,dis数组用来表示该点与A集合中所有点之间的边的长度之和bool vis[MAXN];//用来标记是否该点加入了A集合int Stoer_Wagner(int n){ int i, j, res = INF; for(i = 0; i < n; i ++) v[i] = i; //初始马甲为自己 while(n > 1) { int k, pre = 0; //pre用来表示之前加入A集合的点,我们每次都以0点为第一个加入A集合的点 memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); for(i = 1; i < n; i ++) { k = -1; for(j = 1; j < n; j ++) //根据之前加入的点,要更新dis数组,并找到最大的dis if(!vis[v[j]]) { dis[v[j]] += map[v[pre]][v[j]]; if(k == -1 || dis[v[k]] < dis[v[j]]) k = j; } vis[v[k]] = true;//标记该点已经加入A集合 if(i == n - 1) //最后一次加入的点就要更新答案了 { res = min(res, dis[v[k]]); for(j = 0; j < n; j ++) //将该点合并到pre上,相应的边权就要合并 { map[v[pre]][v[j]] += map[v[j]][v[k]]; map[v[j]][v[pre]] += map[v[j]][v[k]]; } v[k] = v[-- n];//删除最后一个点 } pre = k; } } return res;}int main(){ int n, m, u, v, w; while(scanf("%d%d", &n, &m) != EOF) { memset(map, 0, sizeof(map)); while(m --) { scanf("%d%d%d", &u, &v, &w); map[u][v] += w; map[v][u] += w; } printf("%d\n", Stoer_Wagner(n)); } return 0;}
- POJ 2914 无向图的最小割,模板题。
- poj 2914 Minimum Cut 求无向图的最小割 Stoer-Wagner算法模板
- poj 2914 无向图最小割
- POJ-2914-无向图最小割
- Poj 2914 无向图的全局最小割
- poj 2914 无向图的最小割
- poj2914无向图的最小割模板
- POJ 2914 Minimum Cut //无向图求最小割
- poj 2914 Minimum Cut 无向图最小割
- poj 2914 无向图最小割 Stoer-Wagner算法
- POJ 2914 Minimum Cut 无向图最小割
- poj 2914(无向图最小割Stoer-Wagner算法)
- poj 2914 Minimum Cut(无向图最小割)
- poj 2914 Minimum Cut 无向图最小边割
- 无向图的最小割算法
- 无向图的最小割问题
- POJ 1144 Network Tarjan 求无向图的割点的个数 Tarjan 模板题
- 无向图 最小割
- 人员选择树,搜索自动筛选功能
- Spring相关注解使用总结
- 计算机系统知识
- 对象的肤浅认识和工厂模式
- JavaScript ondragstart、ondragenter、ondragover 、ondrop、ondragend
- POJ 2914 无向图的最小割,模板题。
- 前端面试整理
- 排序算法总结
- HashMap和Hashtable的区别
- cocos2d-x 3.3 中各种动画笔记与实例
- 课堂笔记
- 【单片机】DS18B20
- animation和transition
- VECTOR