思路:

最小生成树是瓶颈生成树,瓶颈生成树满足最大边最小。

数据量较小,所以只需要通过Kruskal,将边按权值从小到大排序,枚举最小边求最小生成树,时间复杂度为O( nm(logm) )

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = + ;
const int inf = 0x3f3f3f3f;
int n, m, pre[maxn];
struct edge{
int s, to, w;
bool operator < ( const edge &a )const{
return w<a.w;
}
} ed[maxn*maxn]; inline int find( int x ){
return pre[x]==x ? x:find(pre[x]);
} inline int kru(int s){
int res = -, cnt = n;
for( int i=; i<=n; i++ ) pre[i] = i;
for( int i=s; i<m; i++ ){
int fx = find(ed[i].s);
int fy = find(ed[i].to);
if( fx!=fy ){
pre[fx] = fy;
cnt --;
if( cnt== ){ //每遍历一条边就少一个点,除第一次外把s点和该边连接的另一边加入,全部;联通时cnt==1而不是cnt==0
res = ed[i].w;
break;
}
}
}
return res-ed[s].w; //不连通时 res==-1 返回值一定小于0
} int main(){
// freopen("in.txt", "r", stdin);
while( ~scanf("%d%d", &n, &m) && (n||m) ){
for( int i=; i<m; i++ )
scanf("%d%d%d", &ed[i].s, &ed[i].to, &ed[i].w);
sort( ed, ed+m );
int ans = kru(); //判断图联不联通
if( ans< ){
puts("-1");
continue;
}
for( int i=; i<m; i++ ){ //枚举最小边,边的编号是从0开始
int tmp = kru(i);
if( tmp< ) break;
if( tmp<ans ) ans = tmp;
}
printf("%d\n", ans);
} return ;
}

poj3522Slim Span(暴力+Kruskal)的更多相关文章

  1. Codeforces Gym 100803F There is No Alternative 暴力Kruskal

    There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...

  2. poj 3522 Slim Span (最小生成树kruskal)

    http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions ...

  3. Slim Span(Kruskal)

    题目链接:http://poj.org/problem?id=3522   Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Subm ...

  4. UVA1395 Slim Span(kruskal)

    题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边, ...

  5. UVALive-3887 Slim Span (kruskal)

    题目大意:定义无向图生成树的最大边与最小边的差为苗条度,找出苗条度最小的生成树的苗条度. 题目分析:先将所有边按权值从小到大排序,在连续区间[L,R]中的边如果能构成一棵生成树,那么这棵树一定有最小的 ...

  6. POJ 3522 Slim Span 暴力枚举 + 并查集

    http://poj.org/problem?id=3522 一开始做这个题的时候,以为复杂度最多是O(m)左右,然后一直不会.最后居然用了一个近似O(m^2)的62ms过了. 一开始想到排序,然后扫 ...

  7. 【BZOJ】【1050】【HAOI2006】旅行comf

    枚举/暴力/Kruskal orz……我sb了……其实是sb题<_< 有一道题问的是最小极差生成树……(不记得是什么名字了,就是求最大边权与最小边权差最小的生成树)做法是枚举最小边,然后k ...

  8. BZOJ2654/COGS1764 [2012国家集训队]tree(陈立杰) [生成树,二分]

    BZOJ传送门,COGS传送门 tree Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V, ...

  9. 公路建设 (highway.c/cpp/pas)

    2.公路建设 (highway.c/cpp/pas) 在滨海市一共有 n 个城市,编号依次为 1 到 n,它们之间计划修建 m 条双向道路,其中 修建第 i 条道路的费用为 ci. 海霸王作为滨海市公 ...

随机推荐

  1. 【MongoDB学习之四】索引 聚合 备份与恢复 监控

    环境 MongoDB 4.0 CentOS 6.5_x64 一.索引语法ensureIndex()方法基本语法格式如下所示:>db.COLLECTION_NAME.ensureIndex({KE ...

  2. 【Spring Boot学习之八】发布打包

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 一.打jar类型1.指定主程序入口,否则运行报错:没有主清单属性pom.xml: <build> < ...

  3. UE4 Cel Shading(卡通渲染)

    转自:https://dawnarc.com/2018/01/ue4cel-shading%E5%8D%A1%E9%80%9A%E6%B8%B2%E6%9F%93/ Cel Shading Post ...

  4. Docker 两键创建 ZeroTier moon 节点

    一条命令创建 ZeroTier moon 节点: $ docker run --name zerotier-moon -d -p 9993:9993 -p 9993:9993/udp seedgou/ ...

  5. Windows下同时安装了Python2与Python3时如何使用RobotFrameWork

    由于windows下不能像linux那样指定python文件的运行路径,当电脑中即安装了python2,又安装了python3时,也不能在环境变量中都配置运行路径吧(当然是可以配置的,系统会按照靠前的 ...

  6. 《ucore lab1 exercise5》实验报告

    资源 ucore在线实验指导书 我的ucore实验代码 题目:实现函数调用堆栈跟踪函数 我们需要在lab1中完成kdebug.c中函数print_stackframe的实现,可以通过函数print_s ...

  7. 6. 运行Spark SQL CLI

    Spark SQL CLI可以很方便的在本地运行Hive元数据服务以及从命令行执行任务查询.需要注意的是,Spark SQL CLI不能与Thrift JDBC服务交互.在Spark目录下执行如下命令 ...

  8. (十一)pdf的构成之文件尾

    件尾部(trailer)如何找到交叉引用表和其他特殊对象 属性: / Size [integer]:指定交叉引用表中的条目数(也计算更新部分中的对象).使用的数字不应是间接参考. / Prev [in ...

  9. 安装macOS时遇到Unable to unmount volume for repair异常导致无法完成安装的解决办法

    方法一: 使用终端命令行制作完macos安装U盘后,务必将.IAProductInfo文件放到U盘的根目录(非EFI分区的) sudo /Applications/Install\ macOS\ Si ...

  10. Actions require unique method/path combination for Swagger

    原文:Actions require unique method/path combination for Swagger services.AddSwaggerGen (c => { c.Re ...