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. Ubuntu之命令壁纸

  2. TextView不用获取焦点也能实现跑马灯

    1.写一个类继承TextView package com.example.tt; import android.content.Context; import android.graphics.Rec ...

  3. 节日(CCF试题)

    试题编号:    201503-3试题名称:    节日时间限制:    1.0s内存限制:    256.0MB问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比 ...

  4. MetaSploit Pro 下载地址

    Windows: https://downloads.metasploit.com/data/releases/metasploit-latest-windows-installer.exe Linu ...

  5. MySQL(二)之服务管理与配置文件修改和连接MySQL

    上一篇给大家介绍了怎么在linux和windows中安装mysql,本来是可以放在首页的,但是博客园说“安装配置类文件”不让放在首页.接下来给大家介绍一下在linux和windows下MySQL的一下 ...

  6. C++ 获取当前时间

    #include <time.h> #include <stdio.h> int main( void ) {     time_t t = time(0);     char ...

  7. 编写Unity3D着色器的三种方式

    不管你会不会写Unity3D的shader,估计你会知道,Unity3D编写shader有三种方式,这篇东西主要就是说一下这三种东西有什么区别,和大概是怎样用的. 先来列一下这三种方式: fixed ...

  8. Unity3D的Time.timeScale

    (1)Time.timeScale = 0可以暂停游戏,Time.timeScale = 1恢复正常,但这是作用于整个游戏的设置,不单单是当前场景,记得在需要的时候重置回Time.timeScale ...

  9. NPOI从数据库中调取数据直接导出到EXCEL中

    关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...

  10. php-fpm配置文件,指定session保存目录

    cd  /etc/php-fpm.d 目录下 cat www.conf文件 修改user ,group 指定session 保存路径 www.conf日志配置路径 php-fpm.conf