hdu3002

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

Problem Description
Zhou xingxing is the successor of one style of kung fu called "Karate Kid".he is falling love with a beautiful judo student,after being humiliated by her boyfriend,a Taekwando master from Japan,Zhou is going to fight with his rival in love.The way they fight
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.
 
Input
The input consists of multiple test cases.

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.
 
Output
There is only one line for each test case, which contains the minimum energy they need to complete this fight.
 
Sample Input
2 1
0 1 50
3 2
0 1 50
1 2 10
 
Sample Output
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算法)的更多相关文章

  1. SW算法求全局最小割(Stoer-Wagner算法)

    我找到的唯一能看懂的题解:[ZZ]最小割集Stoer-Wagner算法 似乎是一个冷门算法,连oi-wiki上都没有,不过洛谷上竟然有它的模板题,并且2017百度之星的资格赛还考到了.于是来学习一下. ...

  2. poj 2914(stoer_wanger算法求全局最小割)

    题目链接:http://poj.org/problem?id=2914 思路:算法基于这样一个定理:对于任意s, t   V ∈ ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Cont ...

  3. HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)

    Problem Description You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa ...

  4. HDU 6081 度度熊的王国战略(全局最小割Stoer-Wagner算法)

    Problem Description 度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族. 哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士. 所以这一场战争,将会十分艰难. 为了更 ...

  5. 全局最小割StoerWagner算法详解

    前言 StoerWagner算法是一个找出无向图全局最小割的算法,本文需要读者有一定的图论基础. 本文大部分内容与词汇来自参考文献(英文,需***),用兴趣的可以去读一下文献. 概念 无向图的割:有无 ...

  6. 全局最小割Stoer-Wagner算法

    借鉴:http://blog.kongfy.com/2015/02/kargermincut/ 提到无向图的最小割问题,首先想到的就是Ford-Fulkerson算法解s-t最小割,通过Edmonds ...

  7. POJ 2914:Minimum Cut(全局最小割Stoer-Wagner算法)

    http://poj.org/problem?id=2914 题意:给出n个点m条边,可能有重边,问全局的最小割是多少. 思路:一开始以为用最大流算法跑一下,然后就超时了.后来学习了一下这个算法,是个 ...

  8. [全局最小割][Stoer-Wagner 算法] 无向图最小割

    带有图片例子的 [BLOG] 复杂度是$(n ^ 3)$ HDU3691 // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragm ...

  9. poj 2914&&hdu 3002 全局最小割Stoer-Wagner算法模板

    #include<stdio.h> #include<string.h> #include<iostream> #define inf 0x3fffffff #de ...

随机推荐

  1. jquery动态分页

    最近一直研究jquery的分页效果,刚刚弄好了一个,拿出来与大家分享.分页效果与时光网的差不多. 网址:http://www.mtime.com/movie/news/all/ 先在aspx页面放置一 ...

  2. 问题解决-某些项目因位于工作空间目录中而被隐藏 & 如何解决java项目导入出错:与另一项目重叠

    有时候我们导入现有的工程时会出现错误,没有继续下一步的那个按钮,错误提示如下:some projects were hidden because they exist in the workspace ...

  3. ThinkPHP重写规则优化URL及Rewrite规则详细说明

    示例如下: http://www.topstack.cn/Article/detail/id/198.html 优化为 http://www.topstack.cn/article-198.html ...

  4. c++ mktime()

    今天联系写一个日历的程序,需要算出月份中的第一天是星期几,用到了mktime()这个函数,感觉这个函数挺有用的,分享给大家. 原型:time_t mktime(struct tm *) 其中的tm结构 ...

  5. datatables隐藏列排序

    var tableOption = { id: 'cacScriptTable', order: [[2, 'desc'],[1, 'desc']],//以第三列‘updatedAt’排序,如果第三列 ...

  6. Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)

    集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...

  7. HTML5开发之 -- 模态突出窗(bootstrap)

    最近在学习web端开发相关,bootstrap非常好用! 有个模态弹出窗的效果,在此记录下: 1.导入: <script src="libs/js/jquery-3.2.1.min.j ...

  8. C# string.Format("{0:C3}", 2)

  9. php检测文件只读、可写、可执行权限

    例子:检测文件是否可读.可写.可执行. 复制代码代码示例: <?php  $myfile = "./test.txt"; if (is_readable ($myfile)) ...

  10. js正则函数match、exec、test、search、replace、split使用介绍集合,学习正则表达式的朋友可以参考下。

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...