【UVA 1395】 Slim Span (苗条树)
【题意】
求一颗生成树,满足最大边和最小边之差最小
Input
The input consists of multiple datasets, followed by a line containing two zeros separated by a space.
Each dataset has the following format.
n m
a1 b1 w1
.
.
.
am bm wm
Every input item in a dataset is a non-negative integer. Items in a line are separated by a space.
n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and
0 ≤ m ≤ n(n − 1)/2. ak and bk (k = 1, . . . , m) are positive integers less than or equal to n, which
represent the two vertices vak
and vbk
connected by the k-th edge ek. wk is a positive integer less than
or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (V, E) is
simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two
or more edges whose both ends are the same two vertices).
Output
For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed.
Otherwise, ‘-1’ should be printed. An output should not contain extra characters.
Sample Input
4 5
1 2 3
1 3 5
1 4 6
2 4 6
3 4 7
4 6
1 2 10
1 3 100
1 4 90
2 3 20
2 4 80
3 4 40
2 1
1 2 1
3 0
3 1
1 2 1
3 3
1 2 2
2 3 5
1 3 6
5 10
1 2 110
1 3 120
1 4 130
1 5 120
2 3 110
2 4 120
2 5 130
3 4 120
3 5 110
4 5 120
5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422
5 8
1 2 1
2 3 100
3 4 100
4 5 100
1 5 50
2 5 50
3 5 50
4 1 150
0 0
Sample Output
1
20
0
-1
-1
1
0
1686
50
【分析】
做完这道题你就变苗条了的意思。。
[沉迷打机,日渐消瘦。
正题->_->先把边排序,枚举最小边,然后就是让最长边最短了咯,就是最小瓶颈生成树,kruskal做最小生成树就好了。
复杂度:m^2
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define Maxn 110 struct node
{
int x,y,c;
}t[Maxn*Maxn];
int n,m; bool cmp(node x,node y) {return x.c<y.c;}
int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} int fa[Maxn];
int ffa(int x)
{
if(fa[x]!=x) fa[x]=ffa(fa[x]);
return fa[x];
} int main()
{
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
for(int i=;i<=m;i++) scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
sort(t+,t++m,cmp);
int cnt=,ans=INF;
for(int i=;i<=m;i++)
{
int cnt=;
for(int j=;j<=n;j++) fa[j]=j;
for(int j=i;j<=m;j++)
{
if(ffa(t[j].x)!=ffa(t[j].y))
{
fa[ffa(t[j].x)]=ffa(t[j].y);
cnt++;
}
if(cnt==n-) {ans=mymin(ans,t[j].c-t[i].c);break;}
}
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
跟LA3887不是一样的么,LA有毒啊狂wa。。蓝书的陈锋的代码也是wa的啊smg!!
2016-11-01 21:24:53
【UVA 1395】 Slim Span (苗条树)的更多相关文章
- UVa 1395 Slim Span【最小生成树】
题意:给出n个节点的图,求最大边减最小边尽量小的值的生成树 首先将边排序,然后枚举边的区间,判定在该区间内是否n个点连通,如果已经连通了,则构成一颗生成树, 则此时的苗条度是这个区间内最小的(和kru ...
- UVa 1395 - Slim Span(最小生成树变形)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1395 Slim Span (最小生成树,MST,kruscal)
题意:给一个图,找一棵生成树,其满足:最大权-最小权=最小.简单图,不一定连通,权值可能全相同. 思路:点数量不大.根据kruscal每次挑选的是最小权值的边,那么苗条度一定也是最小.但是生成树有多棵 ...
- UVa 1395 Slim Span
问题:给出一个n结点的图,求最大边与最小边差值最小的生成树 my code: #include <iostream> #include <cstdio> #include &l ...
- UVa 1395 Slim Span (最小生成树)
题意:给定n个结点的图,求最大边的权值减去最小边的权值最小的生成树. 析:这个和最小生成树差不多,从小到大枚举左端点,对于每一个左端点,再枚举右端点,不断更新最小值.挺简单的一个题. #include ...
- UVA 1395 Slim Span 最小生成树
题意: 给你一个图,让你求这个图中所有生成树中满足题目条件的,这个条件是生成树中最长边与最短边的差值最小. 思路: 根据最小瓶颈生成树的定义:在一个有权值的无向图中,求一个生成树最大边的权值尽量小.首 ...
- UVA - 1395 Slim Span (最小生成树Kruskal)
Kruskal+并查集. 点很少,按边权值排序,枚举枚举L和R,并查集检查连通性.一旦连通,那么更新答案. 判断连通可以O(1),之前O(n)判的,第一次写的过了,后来T.. #include< ...
- 洛谷 UVA1395 苗条的生成树 Slim Span
题目链接 题目描述 求所有生成树中最大边权与最小边权差最小的,输出它们的差值. 题目分析 要求所有生成树中边权极差最小值,起初令人无从下手.但既然要求所有生成树中边权极差最小值,我们自然需要对每一棵生 ...
- UVA1395 Slim Span(kruskal)
题目:Slim Span UVA 1395 题意:给出一副无向有权图,求生成树中最小的苗条度(最大权值减最小权值),如果不能生成树,就输出-1: 思路:将所有的边按权值有小到大排序,然后枚举每一条边, ...
- 最小生成树POJ3522 Slim Span[kruskal]
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7594 Accepted: 4029 Descrip ...
随机推荐
- Mysql统计同一字段不同值的个数
按照 Name 的名字分组,对 Value 值为 0 和 1 的个数进行统计 end) value0, end) value1 from new_table group by name: 结果:
- How to: cgminer (Bitcoin, Litecoin etc.) + AMD Radeon driver install on CentOS
UPDATE 7/7/13: If you want to use Catalyst drivers version 12.8 you will find that X won’t start (er ...
- 启用aspnet
1. 进入dotnetfw目录 cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 2. aspnet_regiis -i -enable
- 20160127 linux 学习笔记
Linux学习笔记第一天 Linux基本介绍 Linux的起源和发展: 简单说linux是一种操作系统,可以安装在包括服务器.个人电脑,乃至PDA.手机.打印机等各类设备中. 起源: Linux起源于 ...
- ACM——回文
回文回文! 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:967 测试通过:338 描述 回文是一种有趣的现 ...
- 那天有个小孩跟我说LINQ(一) 转载
1 LINQ准备(代码下载) 新建项目 linq_Ch1控制台程序,新建一个Entity文件夹 1.1 对象初始化器 在Entity新建一个类Student,代码如下 using S ...
- sql查询结果集根据指定条件排序的方法
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 有几种办法改变这种情况: (1)用 nvl 函数或decode 函数 将null ...
- jQuery对象和Dom对象的区分以及之间转换
刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 一,什么是jQuer ...
- Xcode8 Could not build Objective-C module 'FBSDKCoreKit'
解决方法是: 删除/Users/Rinpe/Library/Developer/Xcode/DerivedData下对应的文件夹即可.
- Telerik柱状图(1)
此随笔主要是介绍一下Telerik的柱状图控件中的一种.效果图为: 此图展示了五个人每个季度的绩效成绩,用图形方式展示数据可以让用户更直观的去看数据,分析数据,不多说了,在这个分享一下我录得视频讲解, ...