最小割Stoer-Wagner算法
最小割Stoer-Wagner算法
割:在一个图G(V,E)中V是点集,E是边集。在E中去掉一个边集C使得G(V,E-C)不连通,C就是图G(V,E)的一个割;
最小割:在G(V,E)的所有割中,边权总和最小的割就是最小割。
求G的任意s-t最小割Min-C(s,t):
设s,t是途中的两个点且边(s,t)∈E(即s,t之间存在一条边)。如果G的最小割Cut把G分成M,N两个点集
①:如果s∈M,t∈N则Min-C(s,t)= Cut(不讨论)
②:如果s,t∈M(或者s,t∈N)则Min-C(s,t)<= Cut
我们来定义一个Contract(a,b)操作,即把a,b两个点合并,表示为删除节点b,把b的节点信息添加到a上,如下图是做了Contract(5,6)
对于所点v有w(v,5)+=w(v,6)
求s-t最小割的方法
定义w(A,x) = ∑w(v[i],x),v[i]∈A
定义Ax为在x前加入A的所有点的集合(不包括x)
1.令集合A={a},a为V中任意点
2.选取V-A中的w(A,x)最大的点x加入集合
3.若|A|=|V|,结束,否则更新w(A,x),转到2
令倒数第二个加入A的点为s,最后一个加入A的点为t,则s-t最小割为w(At,t)
以Poj (pku) 2914 Minimum Cut
的第三个case为例,图为
G(V,E)
我们设法维护这样的一个w[],初始化为0;
我们把V-A中的点中w[i]最大的点找出来加入A集合;
V-A直到为空
w[]的情况如下
w[i] |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
初始值 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
A=A∪{0} |
--- |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
A=A∪{1} |
--- |
2 |
2 |
1 |
0 |
0 |
0 |
|
A=A∪{2} |
--- |
3 |
1 |
0 |
0 |
0 |
||
A=A∪{3} |
--- |
1 |
0 |
0 |
1 |
|||
A=A∪{4} |
--- |
1 |
1 |
2 |
||||
A=A∪{7} |
2 |
2 |
--- |
|||||
A=A∪{5} |
--- |
3 |
||||||
A=A∪{6} |
--- |
每次w[i]+=∑(i,a)的权值a∈A
记录最后加入A的节点为t=6,倒数第二个加入A的为s=5,则s-t的最小割就为w[s],在图中体现出来的意思就是5-6的最小割为w[s]=3
然后我们做Contract(s,t)操作,得到下图
G(V’,E’)
重复上述操作
w[i] |
0 |
1 |
2 |
3 |
4 |
5 |
7 |
初始值 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
A=A∪{0} |
--- |
1 |
1 |
1 |
1 |
0 |
0 |
A=A∪{1} |
--- |
2 |
2 |
1 |
0 |
0 |
|
A=A∪{2} |
--- |
3 |
1 |
0 |
0 |
||
A=A∪{3} |
--- |
1 |
0 |
1 |
|||
A=A∪{4} |
--- |
2 |
2 |
||||
A=A∪{5} |
--- |
4 |
|||||
A=A∪{7} |
--- |
s=5,t=7 s-t最小割是4
Contract(s,t)得到
w[i] |
0 |
1 |
2 |
3 |
4 |
5 |
初始值 |
0 |
0 |
0 |
0 |
0 |
0 |
A=A∪{0} |
--- |
1 |
1 |
1 |
1 |
0 |
A=A∪{1} |
--- |
2 |
2 |
1 |
0 |
|
A=A∪{2} |
--- |
3 |
1 |
0 |
||
A=A∪{3} |
--- |
1 |
1 |
|||
A=A∪{4} |
--- |
4 |
||||
A=A∪{5} |
--- |
s=4,t=5 s-t最小割是4
Contract(s,t)得到
w[i] |
0 |
1 |
2 |
3 |
4 |
初始值 |
0 |
0 |
0 |
0 |
0 |
A=A∪{0} |
--- |
1 |
1 |
1 |
1 |
A=A∪{1} |
--- |
2 |
2 |
1 |
|
A=A∪{2} |
--- |
3 |
1 |
||
A=A∪{3} |
--- |
2 |
|||
A=A∪{4} |
--- |
s=3,t=4 s-t最小割是2,(此时已经得出答案,以下省略)
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int e[maxn][maxn],n,m;
bool comb[maxn];
int Find(int &s,int &t){
bool vis[maxn];
int w[maxn];
memset(vis,false,sizeof(vis));
memset(w,,sizeof(w));
int tmp = INF;
for(int i = ; i < n; ++i){
int theMax = -INF;
for(int j = ; j < n; j++)
if(!vis[j] && !comb[j] && w[j] > theMax)
theMax = w[tmp = j];
if(t == tmp) break;
s = t;
vis[t = tmp] = true;
for(int j = ; j < n; j++)
if(!vis[j] && !comb[j])
w[j] += e[t][j];
}
return w[t];
}
int solve(){
int ans = INF,s,t;
memset(comb,,sizeof(comb));
for(int i = ; i < n; i++){
s = t = -;
ans = min(ans,Find(s,t));
for(int j = ; j < n; j++){
e[s][j] += e[t][j];
e[j][s] += e[j][t];
}
comb[t] = true;
}
return ans;
}
int main() {
int u,v,w;
while(~scanf("%d %d",&n,&m)){
memset(e,,sizeof(e));
while(m--){
scanf("%d %d %d",&u,&v,&w);
e[u][v] += w;
e[v][u] += w;
}
printf("%d\n",solve());
}
return ;
}
转载自http://blog.sina.com.cn/s/blog_700906660100v7vb.html
最小割Stoer-Wagner算法的更多相关文章
- POJ 2914 Minimum Cut Stoer Wagner 算法 无向图最小割
POJ 2914 题意:给定一个无向图 小于500节点,和边的权值,求最小的代价将图拆为两个联通分量. Stoer Wagner算法: (1)用类似prim算法的方法求"最大生成树" ...
- 图的全局最小割的Stoer-Wagner算法及例题
Stoer-Wagner算法基本思想:如果能求出图中某两个顶点之间的最小割,更新答案后合并这两个顶点继续求最小割,到最后就得到答案. 算法步骤: --------------------------- ...
- 求全局最小割(SW算法)
hdu3002 King of Destruction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 网络流 最大流—最小割 之SAP算法 详解
首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...
- poj 2914(stoer_wanger算法求全局最小割)
题目链接:http://poj.org/problem?id=2914 思路:算法基于这样一个定理:对于任意s, t V ∈ ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Cont ...
- CQOI2016 不同的最小割 (最小割树模板)(等价流树的Gusfield构造算法)
题目 最小割树模板 算法详解及证明见: 2016年国家队候选队员论文 <浅谈无向图最小割问题的一些算法及应用--绍兴一中 王文涛> 3.2节 CODE #include <bits/ ...
- ZJOI 最小割 CQOI 不同的最小割 (最小割分治)
题目1 ZJOI 最小割 题目大意: 求一个无向带权图两点间的最小割,询问小于等于c的点对有多少. 算法讨论: 最小割 分治 代码: #include <cstdlib> #include ...
- UVALive 5099 Nubulsa Expo 全局最小割问题
B - Nubulsa Expo Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- poj2914 Minimum Cut 全局最小割模板题
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 8324 Accepted: 3488 Case ...
- POJ2914 (未解决)无向图最小割|Stoer-Wagner算法|模板
还不是很懂,贴两篇学习的博客: http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html http://blog.sina.c ...
随机推荐
- 单片机显示原理(LCD1602)
一.接口 LCD1602是很多单片机爱好者较早接触的字符型液晶显示器,它的主控芯片是HD44780或者其它兼容芯片.与此相仿的是LCD12864液晶显示器,它是一种图形点阵显示器,能显示的内容比LCD ...
- Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on proje
Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on proje ...
- UVA 10888 - Warehouse(二分图完美匹配)
UVA 10888 - Warehouse option=com_onlinejudge&Itemid=8&page=show_problem&category=562& ...
- Qt中暂停线程的执行(主线程和工作线程共用一把锁,一旦主线程将它锁上,工作线程就无法运行了,这也是一个办法)
在线程中定义一个信号量: QMutex pause; 把run()函数中循环执行的部分用信号量pause锁住: void run() { while(1) { pause.lock ...
- 让ubuntu支持GBK编码AAAAA
Eclipse 添加GBK编码 首先Windows->Preferences, 然后选择General下面的Workspace. Text file encoding选择Other GBK, 如 ...
- Adding a model
https://docs.asp.net/en/latest/tutorials/first-mvc-app/adding-model.html Adding data model classes I ...
- Codeforces Round #454
Masha and Bears Tic-Tac-Toe Shockers Seating of Students Party Power Tower Reverses
- 7) 十分钟学会android--Activity的生命周期之暂停与恢复
在正常使用app时,前端的activity有时会被其他可见的组件阻塞(obstructed),从而导致当前的activity进入Pause状态.例如,当打开一个半透明的activity时(例如以对话框 ...
- 【Oracle】审计
1.审计的功能:监控用户在database 的 action (操作) 2.审计分类: 1) session :在同一个session,相同的语句只产生一个审计结果(默认) 2) access : 在 ...
- WIN系统查询版本
cmd -> DISM /online /Get-CurrentEdition //查询系统版本 WIN+R -> slmgr.vbs -ipk 查询系统注册信息slmgr.vbs -dl ...