POJ2914 Minimum Cut —— 最小割
来源:互联网 发布:好的职业技术学校知乎 编辑:程序博客网 时间:2024/06/05 07:29
题目链接:http://poj.org/problem?id=2914
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 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 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>#include <algorithm>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <string>#include <set>using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int MAXN = 500+10;int mp[MAXN][MAXN];bool combine[MAXN];int n, m;bool vis[MAXN];int w[MAXN];int prim(int times, int &s, int &t) //最大生成树?{ memset(w,0,sizeof(w)); memset(vis,0,sizeof(vis)); for(int i = 1; i<=times; i++) //times为实际的顶点个数 { int k, maxx = -INF; for(int j = 0; j<n; j++) if(!vis[j] && !combine[j] && w[j]>maxx) maxx = w[k=j]; vis[k] = 1; s = t; t = k; for(int j = 0; j<n; j++) if(!vis[j] && !combine[j]) w[j] += mp[k][j]; } return w[t];}int mincut(){ int ans = INF; memset(combine,0,sizeof(combine)); for(int i = n; i>=2; i--) //每一次循环,就减少一个点 { int s, t; int tmp = prim(i, s, t); ans = min(ans, tmp); combine[t] = 1; for(int j = 0; j<n; j++) //把t点删掉,与t相连的边并入s { mp[s][j] += mp[t][j]; mp[j][s] += mp[j][t]; } } return ans;}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(mp,0,sizeof(mp)); for(int i = 1; i<=m; i++) { int u, v, w; scanf("%d%d%d",&u,&v,&w); mp[u][v] += w; mp[v][u] += w; } cout<< mincut() <<endl; } return 0;}
- POJ2914 Minimum Cut —— 最小割
- poj2914——Minimum Cut//最小割Stoer_Wagner
- POJ2914 Minimum Cut 最小割集
- POJ2914 Minimum Cut(最小割模板题)
- POJ2914 Minimum Cut(最小割)
- 全局最小割 poj2914 Minimum Cut
- POJ2914 Minimum Cut 【全局最小割】(Stoer_Wagner)
- POJ2914 Minimum Cut【全局最小割】【Stoer-Wangner】
- 【POJ2914】Minimum Cut-无向图的全局最小割
- POJ2914 Minimum Cut 【全局最小割Stoer-Wagner模板题】
- POJ2914 Minimum Cut(无向图的最小割,Stoer_Wagner算法)
- POJ2914:Minimum Cut
- hdu6214—Smallest Minimum Cut(最小割的最少割边)
- poj2914 全局最小割
- POJ 2914 Minimum Cut 全局最小割
- POJ 2914 Minimum Cut 最小割
- 【最小割】POJ-2914 Minimum Cut
- poj 2914 Minimum Cut(全局最小割)
- 剑指offer-字符流中第一个不重复的字符
- GDB调试时候的问题
- 2017迅雷秋招笔试题-点是否在三角形内
- 初步了解JFinal
- 心得——来自大牛的建议1
- POJ2914 Minimum Cut —— 最小割
- 半年总结
- 页面导入样式时,使用link和@import有什么区别?
- 驱动学习回顾——按键(Button)驱动的理解和总结
- 通信协议——I2C总线
- 专注
- 洛谷 P1195 口袋的天空
- 生产者/消费者问题的多种Java实现方式
- React Native移动开发实战-4-Android平台的适配原理