思路:对所有路径的速度从小到大排个序,然后枚举高度差就ok......

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 1<<30
struct node
{
int v1,v2;
int dis;
}s[2000];
int father[2000];
int find(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
int cmp(const node a,const node b)
{
if(a.dis<b.dis)
return 1;
else return 0;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)>0)
{
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&s[i].v1,&s[i].v2,&s[i].dis);
}
sort(s,s+m,cmp);
int q;
scanf("%d",&q);
while(q--)
{
int tmp,tmp1;
int minx=M;
scanf("%d%d",&tmp,&tmp1);
for(int i=0;i<m;i++)
{
for(int k=0;k<=n;k++)
father[k]=k;
for(int j=i;j<m;j++)
{
int x=find(s[j].v1);
int y=find(s[j].v2);
if(x!=y)
father[x]=y;
if(find(tmp)==find(tmp1))
{
if(minx>(s[j].dis-s[i].dis))
{
minx=s[j].dis-s[i].dis;
}
break;
}
}
}
if(minx>=M)
printf("-1\n");
else
printf("%d\n",minx);
}
}
return 0;
}

hdu1598的更多相关文章

  1. 最舒适的路(并查集+枚举)(hdu1598)

    hdu1598 find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768 ...

  2. *HDU1598 并查集

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU-1598 find the most comfortable road

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. hdu-1598

    思路: 首先如果想到了Kruskal算法,那么下一步我们可能马上会想:那我们就从头开始写这个算法吧.然后一写就很容易发现一个问题——如果按照正常的Kruskal算法来做,那么start到end的舒适度 ...

  5. 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】

    题意:  给你一个图,有边权,K个询问:u到v 的路径中   边权最大值-边权最小值的最小值是多少 http://acm.hdu.edu.cn/showproblem.php?pid=1598 题解( ...

  6. HDU1598 find the most comfortable road 【并查集】+【枚举】

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. hdu1598 find the most comfortable road (枚举)+【并查集】

    <题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...

  8. [HDU1598]find the most comfortable road

    思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...

  9. find the most comfortable road(hdu1598)不错的并查集

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. 手动修改magento域名

    So it turns out the problem was that Apache didn't have write permissions to the WEBROOT/var directo ...

  2. 【java】详解java中的注解(Annotation)

    目录结构: contents structure [+] 什么是注解 为什么要使用注解 基本语法 4种基本元注解 重复注解 使用注解 运行时处理的注解 编译时处理的注解 1.什么是注解 用一个词就可以 ...

  3. 【Oracle】详解ORACLE中的trigger(触发器)

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

  4. Linux导出/导入逻辑卷组信息

    源主机上操作: 将文件系统umount # umount /u01 再将LV和VG inactive: # lvchange -an /dev/vg_u01/lv_u01 # vgchange -an ...

  5. Delphi GDI对象之剪切区域

    原文链接: http://www.cnblogs.com/pchmonster/archive/2012/07/05/2577627.html 剪切区域(Clipping Regions) Regio ...

  6. vmware虚拟机三种网络连接方式

    一.概述 vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式). 打开vmware虚拟机,我们可以在选项栏的“ ...

  7. 转 Kubernetes 入门 概念理解

    你闺女也能看懂的插画版Kubernetes指南 原创  2016-06-30 作者 周小璐 译 编者按:Matt Butcher是Deis的平台架构师,热爱哲学,咖啡和精雕细琢的代码.有一天女儿走进书 ...

  8. Python 爬虫 解决escape问题

    爬取某个国外的网址,遇到的编码问题 ,在前段页面 返回的数据是 亞洲私人珍藏賣,令仝好分享他為此 所傾注的心血與熱愛。 爬虫源码是: url = 'http://www.bonhams.com/auc ...

  9. spring中InitializingBean接口使用理解(转)

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  10. MySQL通过Explain查看select语句的执行计划结果触发写操作

    [背景] 某某同学执行了一下Explain结果结果发现数据库有了一条写入操作,恭喜这位同学你的锅到货了,你签收一下: 对! 你没有听错,在一种场景下就算是Explain也会引发数据的写操作,就这是外层 ...