【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 ...
随机推荐
- HTTP请求、响应报文格式
HTTP请求报文格式: HTTP请求报文主要由请求行.请求头部.空行以及请求正文4部分组成 1,请求行由3部分组成,分别为:请求方式,URI(注意这里不是URL)以及协议版本组成,之间由空格分隔 请求 ...
- Java_LIST使用方法和四种遍历arrayList方法
1.List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象. List接口的常用实现类有ArrayList和Linked ...
- Ext.Net学习笔记09:Ext.Net Store的用法
使用Handler处理分页 首先来创建一般处理程序,我命名为StoreHandler.ashx,然后它的处理过程代码如下: public void ProcessRequest(HttpContext ...
- java新手笔记18 类比较
1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 // ...
- [USACO1.1.4]坏掉的项链Broken Necklace
P1203 [USACO1.1]坏掉的项链Broken Necklace 标签 搜索/枚举 USACO 难度 普及- 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N&l ...
- 使用Idea编写javaweb以及maven的综合(一)
今天总结的第一点是在windows下使用idea编写jsp并且使用tomcat部署:第二点是新建maven项目,之前一直是听说也没有自己实践过,今天就大概说一下. 0x01 IDEA 全称 Intel ...
- Flash Professional CS6 安装zxp插件
说明 头两天因工作原因需要使用DragonBones,他的工作方式是的Flash Professional CS5.5以上的环境. DragonBones提供的是一个文件名为:xzp的文件,在Wind ...
- zlib压缩解压示例
#include <stdio.h> #include <string.h> #include <assert.h> #include "zlib.h&q ...
- (转 部分修改) IOS 手势密码(简单版)
// // Created by wangtouwang on 15/4/7. // Copyright (c) 2015年 wangtouwang. All rights reserved. // ...
- HTML教程:link标记
开发php语言的网站,<head>里link标签这样:<link href="xmlrpc.php?rsd=1" title="rsd" ty ...