Problem Description

XX星有很多城市,城市之间通过一种奇怪的快速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流。每条SARS都对行驶在上面的Flycar限制了固定的Speed,同一时候XX星人对 Flycar的“舒适度”有特殊要求,即乘坐过程中最快速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速。痛苦呀 ),

但XX星人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。

(SARS是双向的)。

Input

输入包含多个測试实例,每一个实例包含:

第一行有2个正整数n (1<n<=200)和m (m<=1000),表示有N个城市和M条SARS。

接下来的行是三个正整数StartCity,EndCity,speed,表示从表面上看StartCity到EndCity,限速为speedSARS。speed<=1000000

然后是一个正整数Q(Q<11),表示寻路的个数。

接下来Q行每行有2个正整数Start,End, 表示寻路的起终点。

Output

每一个寻路要求打印一行。仅输出一个非负整数表示最佳路线的舒适度最快速与最低速的差。假设起点和终点不能到达。那么输出-1。

Sample Input

4 4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2

Sample Output

1
0

# include<iostream>
# include<cstdio>
# include<algorithm>
using namespace std;
const int maxn=1000+5;
struct node{
int st;
int ed;
int fu;
}road[maxn];
int p[maxn];
int st,ed;
int miin;
int inf=1000000+5;
bool cmp(node x,node y)
{
return x.fu<y.fu;
}
int find(int x)
{
return p[x]== x ? x:p[x]=find(p[x]);
} int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=m;i++)
scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu);
sort(road+1,road+m+1,cmp);
int tot;
cin>>tot;
while(tot--)
{
miin=inf;
scanf("%d%d",&st,&ed);
for(int i=1;i<=m;i++)
{
for(int i=1;i<=n;i++) p[i]=i;
for(int j=i;j<=m;j++)
{
int x=find(road[j].st);
int y=find(road[j].ed);
if(x!=y) p[x]=y;
if(find(st)==find(ed))
{
int sum=road[j].fu-road[i].fu;
if(sum<miin) miin=sum;
break;
}
}
}
if(miin==inf)
printf("-1\n");
else
printf("%d\n",miin);
} }
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1598 find the most comfortable road (罗列+Kruskal) 并检查集合的更多相关文章

  1. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  2. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  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 find the most comfortable road (并查集)

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

  5. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  6. HDU 1598 find the most comfortable road (MST)

    find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 1598 find the most comfortable road(并查集+枚举)

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

  8. HDU - 1598 find the most comfortable road 【最小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...

  9. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

随机推荐

  1. Latex表格制作记录

    Latex表格制作记录 主要功能 合并表格的行列 长表格的使用 makecell例程借鉴 效果图 参考代码 \documentclass{ctexart} \usepackage{indentfirs ...

  2. localStorage兼容方案

    localStorage是H5的存储方案,各大浏览器支持都相当不错,唯一悲催的就是IE,这个浏览器界的另类总是显得格格不入. IE “Internet选项”->“安全”中有一个“启动保护模式”的 ...

  3. 学习Numpy

    1.什么是numpy NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多( ...

  4. Android多线程研究(1)——线程基础及源码剖析

    从今天起我们来看一下Android中的多线程的知识,Android入门容易,但是要完成一个完善的产品却不容易,让我们从线程开始一步步深入Android内部. 一.线程基础回顾 package com. ...

  5. 《从零開始学Swift》学习笔记(Day 1)——我的第一行Swift代码

     Swift 2.0学习笔记--我的第一行Swift代码 原创文章,欢迎转载. 转载请注明:关东升的博客 当第一次看到以下代码时我石化了.这些代码是什么东东?单词拼出来的? import Foun ...

  6. Linux下使用Python的Tkinter库出现的No module named _tkinter问题

    这是由于python的版本没有包含tkinter的模块,只需要把tk的package安装就可以了. 一般在linux才出现,windows版本一般已经包含了tkinter模块.

  7. .NET-架构优化实战-底层服务优化

    原文:.NET-架构优化实战-底层服务优化 前言 问题分析 在本系列第一篇文章我们提到,底层问题主要存在以下两点: 代码冗余 时效低 代码冗余 例如: 领奖方法不统一,一次性的写一套,可循环的又写一套 ...

  8. 编辑器sublime、终端运行python

    sublime编辑器 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用) Sublime Text是由程序员Jon Skinner于2008年1月份 ...

  9. oracle 中日期的加减

    oracle 中日期的加减 加法   select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_ ...

  10. RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项

    RESET MASTER 删除所有index file 中记录的所有binlog 文件,将日志索引文件清空,创建一个新的日志文件,这个命令通常仅仅用于第一次用于搭建主从关系的时的主库,注意  rese ...