Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 8324   Accepted: 3488
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 ≤ MN × (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, AB, 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 555
#define inf 1<<30 int v[MAXN],dist[MAXN];
int map[MAXN][MAXN];
bool vis[MAXN];
int n,m; //求全局最小割的Stoer_Wanger算法
int Stoer_Wanger(int n)
{
int res=inf;
for(int i=;i<n;i++)v[i]=i;
while(n>){
int k=,pre=;//pre用来表示之前加入A集合的点,我们每次都以0点为第一个加入A集合的点
memset(vis,false,sizeof(vis));
memset(dist,,sizeof(dist));
for(int i=;i<n;i++){
k=-;
for(int j=;j<n;j++){
if(!vis[v[j]]){
dist[v[j]]+=map[v[pre]][v[j]];//dis数组用来表示该点与A集合中所有点之间的边的长度之和
if(k==-||dist[v[k]]<dist[v[j]]){
k=j;
}
}
}
vis[v[k]]=true;
if(i==n-){
res=min(res,dist[v[k]]);
//将该点合并到pre上,相应的边权就要合并
for(int j=;j<n;j++){
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 u,v,w,ss;
while(~scanf("%d%d",&n,&m)){ memset(map,,sizeof(map));
while(m--){
scanf("%d%d%d",&u,&v,&w); map[u][v]+=w;
map[v][u]+=w;
}
int ans=Stoer_Wanger(n);
printf("%d\n",ans);
}
return ;
}
 

poj2914 Minimum Cut 全局最小割模板题的更多相关文章

  1. POJ 2914 Minimum Cut 全局最小割

    裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...

  2. POJ 2914 Minimum Cut【最小割 Stoer-Wangner】

    题意:求全局最小割 不能用网络流求最小割,枚举举汇点要O(n),最短增广路最大流算法求最大流是O(n2m)复杂度,在复杂网络中O(m)=O(n2),算法总复杂度就是O(n5):就算你用其他求最大流的算 ...

  3. hdu 6214 Smallest Minimum Cut(最小割的最少边数)

    题目大意是给一张网络,网络可能存在不同边集的最小割,求出拥有最少边集的最小割,最少的边是多少条? 思路:题目很好理解,就是找一个边集最少的最小割,一个方法是在建图的时候把边的容量处理成C *(E+1 ...

  4. 2017青岛赛区网络赛 Smallest Minimum Cut 求最小割的最小割边数

    先最大流跑一遍 在残存网络上把满流边容量+1 非满流边容量设为无穷大 在进行一次最大流即可 (这里的边都不包括建图时用于反悔的反向边) #include<cstdio> #include& ...

  5. 全局最小割模板(定S,不定T,找最小割)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  6. UVALive 5099 Nubulsa Expo(全局最小割)

    题面 vjudge传送门 题解 论文题 见2016绍兴一中王文涛国家队候选队员论文<浅谈无向图最小割问题的一些算法及应用>4节 全局最小割 板题 CODE 暴力O(n3)O(n^3)O(n ...

  7. POJ 2914 Minimum Cut (全局最小割)

    [题目链接] http://poj.org/problem?id=2914 [题目大意] 求出一个最小边割集,使得图不连通 [题解] 利用stoerwagner算法直接求出全局最小割,即答案. [代码 ...

  8. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  9. POJ 2914 - Minimum Cut - [stoer-wagner算法讲解/模板]

    首先是当年stoer和wagner两位大佬发表的关于这个算法的论文:A Simple Min-Cut Algorithm 直接上算法部分: 分割线 begin 在这整篇论文中,我们假设一个普通无向图G ...

随机推荐

  1. ABAP Netweaver和git的快捷方式

    Netweaver Jerry的SAPGUI收藏夹管理工具:链接 git 我笔记本上有很多github仓库,每次切换仓库,我不想敲很长的cd命令.比如现在我需要手敲下面的命令进入一个Java仓库: c ...

  2. 【BZOJ1076】[SCOI2008] 奖励关(状压DP)

    点此看题面 大致题意:总共有\(n\)个宝物和\(k\)个回合,每个回合系统将随机抛出一个宝物(抛出每个宝物的概率皆为\(1/n\)),吃掉一个宝物可以获得一定的积分(积分可能为负),而吃掉某个宝物有 ...

  3. 查看电脑是否安装node.js

    打开命令行

  4. nodejs 实现图片上传

    1.首先在目录下的运行cmd,执行以下命令 npm install multer; 2.在router下新建upload.js let express = require('express');let ...

  5. Smallest Common Multiple-freecodecamp算法题目

    Smallest Common Multiple 1.要求 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 2.思路 设定一个twoMultiple(a,b)函数,求出输入两个参数的最小公 ...

  6. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. Springboot 入门创建hello world1!

    1.首先使用工具是Eclipse,安装插件,点击“Help”-“Eclipse Marketplace...”, 一步步直接Ok,等待安装完成 2.创建Springboot项目 到此 就创建成功了 3 ...

  8. 工具之UltraEdit之正则表达式

  9. Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...

  10. Git笔记(流水账)

    命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况: 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修 ...