【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 ...
随机推荐
- 转:maven报错非法字符:\65279 错误
开发中一个项目很早就报这个错,maven报错非法字符:\65279 错误,今天终于忍无可忍要解决它 :编译java文件的时候,有些java文件报非法字符 \65279错误,在网上找和很多 方法,也试了 ...
- 第十一篇:web之Django之Form组件
Django之Form组件 Django之Form组件 本节内容 基本使用 form中字段和插件 自定义验证规则 动态加载数据到form中 1. 基本使用 django中的Form组件有以下几个功 ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- ASP.NET Identity 用户注册相关设定
此部分可以在 Web项目中的App_Start目录下的 IdentityConfig.cs 文件进行设置. 1.配置密码的验证逻辑 manager.PasswordValidator = new Pa ...
- Js~数组的操作push,pop,shift,unshift
说几个概念: 队列:先进先出堆栈:先进后出 shift:从集合中把第一个元素删除,返回这个元素的值pop:从集合中把最后一个元素删除,返回这个元素的值 unshift:在集合开头添加一个或者多个元素, ...
- HOOK函数(二)——全局HOOK
如果钩子函数定义于当前进程相关的线程中,则钩子函数只能处理当前进程的线程的消息,如果要想处理当前正在运行的所有进程的鼠标消息和键盘消息,那么安装钩子函数的代码必须实现在动态链接库中.所以如果想让安装的 ...
- LIBPNG使用小结(二)
之前写的LIBPNG库学习小结介绍了怎么样自定义LIBPNG库的write.read.flush函数,而不使用LIBPNG库提供的默认函数. 上一篇讲述的都是在单线程的情况下,今天将程序升级,放在多线 ...
- ios PullToRefresh using animated GIF or image array or Vector image
说说那些令人惊叹的下拉效果 1. 动画下拉,这里借用一下github的资源 优点:直接用gif图处理,下拉进度完全按照gif图运行时间,只要时间和下拉进度匹配就可以了, 效果很流畅 https://d ...
- dapper的一个小扩展以支持dataset
废话不多,直接上方法 public static DataSet ExecuteDataSet(this IDbConnection cnn, IDbDataAdapter adapter, stri ...
- Headfirst设计模式的C++实现——装饰者模式(Decorater)
Beverage.h #ifndef BEVERAGE_H_INCLUDED #define BEVERAGE_H_INCLUDED #include <string> class Bev ...