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. 程序猿必备软件转载自 www.uhdesk.com

    XMLSpy 2012 企业版中文破解版 软件描写叙述: XMLSpy是XML(标准通用标记语言的子集)编辑器,支持WYSWYG.支持Unicode.多字符集,支持Well-formed和Valida ...

  2. GCD 初步学习

    GCD应该是比較牛逼的东西了吧,一时半会应该是操作不好. 在cocoa-china上面有两篇关于GCD的文章.GCD 深入理解(一) GCD 深入理解(二) CSDN荣芳志博客:点击打开链接 我仅仅是 ...

  3. "网络适配器本地连接没有有效ip地址配置"错误的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 win server2008通过一台win xp的共享网络来上网,采用ipv4,网址设置如下: win xp是可以上网 ...

  4. Xcode5新特性之注释

    Xcode5新特性之注释 Xcode5在注释式文档方面也有进步,越来越象javadoc. Xcode4 参考一下教程 http://blog.chukong-inc.com/index.php/201 ...

  5. 具体解释https是怎样确保安全的

    Https 介绍 什么是Https HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer).是以安全为目标的HTTP通道,简单讲是 ...

  6. tomcat 启动项目时出现 ZipException: error in opening zip file

    错误情况 项目用 maven 打好 war 包后放到 tomcat 下,启动 tomcat,出现以下错误 3-Nov-2017 12:21:44.346 严重 [localhost-startStop ...

  7. USB 3.0规范中译本 第10章 集线器,主机下行口以及设备上行口规范

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章描述USB 3.0 集线器的体系结构要求.本章还描述主机下行口和集线器下行口之间功能性的不同之处,以及设 ...

  8. 用SQL找出前N名

    业务系统中常常会有排名的需求,考试和比赛中则更普遍了.Excel 中也有个 Rank 函数供排名之用,数据库中更不例外了. 如果须要找出工资最高的前三个员工工资(及其员工号). 只是."前三 ...

  9. 博客搬家啦! -----> http://ronghaopger.github.io/

    新地方: http://ronghaopger.github.io/ 以后这里就不更新了,感谢博客园!

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

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