求全局最小割(SW算法)
King of Destruction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1022 Accepted Submission(s): 400
is to destroy the wooden plank between some wooden pegs,in order to cut these wooden pegs into two disconnected parts,and destroy each piece of plank need consume different energy.However Zhou xingxing is beginner after all,so he is turn to you for help,please
calculate the minimum energy he need to destroy the wooden plank.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
2 1
0 1 50
3 2
0 1 50
1 2 10
50
10
题意:求无向图的最小割。思路:无向图的最小割又叫全局最小割,枚举汇点求最大流效率很低,因而普遍使用StoerWagner算法,时间复杂度为O(n^3)。程序:#include"stdio.h"
#include"string.h"
#define inf 99999999
int G[666][666],ans;
int min(int a,int b)
{
return a<b?a:b;
}
int f[666];
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
f[x]=y;
}
void Mincut(int n)
{
ans=inf;
int node[666],dis[666];
bool use[666];
int i,k,pre;
for(i=0;i<n;i++)
node[i]=i;
while(n>1)
{
int maxj=1;
for(i=1;i<n;i++)//初始化到已圈集合的割大小
{
dis[node[i]]=G[node[i]][node[0]];
if(dis[node[maxj]]<dis[node[i]])
{
maxj=i;
}
}
pre=0;
memset(use,false,sizeof(use));
use[node[0]]=true;
for(k=1;k<n;k++)
{
if(k==n-1)//只剩最后一个没加入集合的点,更新最小割
{
ans=min(ans,dis[node[maxj]]);
for(i=0;i<n;i++)//合并最后一个点以及推出它的集合中的点
{
G[node[i]][node[pre]]=G[node[pre]][node[i]]+=G[node[i]][node[maxj]];
}
node[maxj]=node[--n];//缩点后的图
}
use[node[maxj]]=true;
pre=maxj;
maxj=-1;
for(i=1;i<n;i++)
{
if(!use[node[i]])
{
dis[node[i]]+=G[node[i]][node[pre]];//将上次求的maxj加入集合,合并与它相邻的边到割集
if(maxj==-1||dis[node[maxj]]<dis[node[i]])
maxj=i;
}
}
}
}
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=-1)
{
memset(G,0,sizeof(G));
for(i=0;i<n;i++)
{
f[i]=i;
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a][b]=G[b][a]+=c;
make(a,b);
}
int flag=0;
for(i=0;i<n;i++)
{
if(finde(i)!=finde(0))
flag++;
}
if(flag)
printf("0\n");
else
{
Mincut(n);
printf("%d\n",ans);
} }
}
求全局最小割(SW算法)的更多相关文章
- SW算法求全局最小割(Stoer-Wagner算法)
我找到的唯一能看懂的题解:[ZZ]最小割集Stoer-Wagner算法 似乎是一个冷门算法,连oi-wiki上都没有,不过洛谷上竟然有它的模板题,并且2017百度之星的资格赛还考到了.于是来学习一下. ...
- poj 2914(stoer_wanger算法求全局最小割)
题目链接:http://poj.org/problem?id=2914 思路:算法基于这样一个定理:对于任意s, t V ∈ ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Cont ...
- HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)
Problem Description You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa ...
- HDU 6081 度度熊的王国战略(全局最小割Stoer-Wagner算法)
Problem Description 度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族. 哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士. 所以这一场战争,将会十分艰难. 为了更 ...
- 全局最小割StoerWagner算法详解
前言 StoerWagner算法是一个找出无向图全局最小割的算法,本文需要读者有一定的图论基础. 本文大部分内容与词汇来自参考文献(英文,需***),用兴趣的可以去读一下文献. 概念 无向图的割:有无 ...
- 全局最小割Stoer-Wagner算法
借鉴:http://blog.kongfy.com/2015/02/kargermincut/ 提到无向图的最小割问题,首先想到的就是Ford-Fulkerson算法解s-t最小割,通过Edmonds ...
- POJ 2914:Minimum Cut(全局最小割Stoer-Wagner算法)
http://poj.org/problem?id=2914 题意:给出n个点m条边,可能有重边,问全局的最小割是多少. 思路:一开始以为用最大流算法跑一下,然后就超时了.后来学习了一下这个算法,是个 ...
- [全局最小割][Stoer-Wagner 算法] 无向图最小割
带有图片例子的 [BLOG] 复杂度是$(n ^ 3)$ HDU3691 // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragm ...
- poj 2914&&hdu 3002 全局最小割Stoer-Wagner算法模板
#include<stdio.h> #include<string.h> #include<iostream> #define inf 0x3fffffff #de ...
随机推荐
- tp-03 模板显示
方式模板:$this->display(); 每当建立一个控制器 都要在view建立一个名字相对应的文件夹 在建立相对于的页面.
- ASP.NET C# 获取当前日期 时间 年 月 日 时 分 秒
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- mysql -- 重装mysql失败的解决办法
最近遇到一些问题,将mysql卸载了重装,但总是出现安装不成功,应该是上一个mysql没卸载干净,于是各种找资料,前后弄了几个小时,终于给弄出来了,结合网上的资料,现总结如下: 1. 打开控制面板-添 ...
- Asakura的魔法世界
Font Size:Aa Aa Aa Description Asakura存在于一个魔法世界中.有一天,Asakura在一条魔法通道里偷懒,突然接到一个紧急任务,要高速赶往还有一条通道b去. 我们把 ...
- link with editor
在左侧explore上,有个双向的箭头,点一下,就会把路径和当前文件自动对应
- SQL Server 删除数据库所有表和所有存储过程
场景: SQL Server中,需要删除所有表或所有存储过程时,手动的方式只能逐个进行删除,耗个人时间,所以想弄个语句来实现这样的需求. 如果由于外键约束删除table失败,则先删除所有约束: - ...
- cesium可视化空间数据1
---恢复内容开始--- 1.多边形 我们要从经度和纬度列表中为美国怀俄明州添加一个多边形.(怀俄明被选中是因为它是一个简单的多边形.)我们可以复制并粘贴以下代码到Sandcastle中: < ...
- 京东云擎”本周四推出一键免费安装Discuz论坛
“京东云擎”本周四推出一键免费安装Discuz论坛了,让用户能在1分钟之内建立自己的论坛.这是继上周云擎推出一键安装WordPress之后的又一重大免费贡献! 云擎: http://jae.jd.co ...
- nagios监控mysql
在nagios上部署check_mysql_health 监控mysql 博客分类: 架构 本监控为基于nagios服务器主动监控方法,利用check_mysql_health实现多种监控模式: ...
- linux命令在文件中根据命令查找
find . -type f -name "*.tmp" | xargs grep -ri "2016-08-30 04:00:00|2016-08-30 05:00:0 ...