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. IIS7配置PHP图解

    IIS6整合PHP详解:http://zhidao.zgsj.com/article/8/2011118142648.shtml PHP5.2.17 官方下载: http://windows.php. ...

  2. 【转】HTML5移动端最新兼容问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显 ...

  3. 数据类型总结——Array(数组类型)

    相关文章 简书原文:https://www.jianshu.com/p/1e4425383a65 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  4. UE4 Editor快捷键(ShortCut Key)

    转载请注明出处,所有权利保留. Unreal Engine4的快捷键现在无官方文档,因为他们工作比较忙啊. 记录时间:2014-10-15 现在自己整理一个,仅供参考. 因为他们的team成员说的还有 ...

  5. 简要分析武汉一起好P2P平台的核心功能

    写作背景 加入武汉一起好,正式工作40天了,对公司的核心业务有了更多的了解,想梳理下自己对于P2P平台的认识. 武汉一起好,自己运营的yiqihao.com,是用PHP实现的,同时也帮助若干P2P平台 ...

  6. java根据xml配置文件导出excel通用方法

    java web项目中时常会用到导出功能,而导出excel几乎是每个项目必备的功能之一.针对形形色色的导出方法及个人平时的工作经验,特将导出excel方法整理成通用的方法,根据xml配置来实现特定的导 ...

  7. [Recompose] Replace a Component with Non-Optimal States using Recompose

    Learn how to use the ‘branch’ and ‘renderComponent’ higher-order components to show errors or messag ...

  8. gcc编译选项--转

    gcc提供了大量的警告选项,对代码中可能存在的问题提出警告,通常可以使用-Wall来开启以下警告:           -Waddress -Warray-bounds (only with -O2) ...

  9. .gitignore 设置忽略上传的文件

    首先在一个项目中新建如下所示文件用来测试 image.png 一.生成.gitignore文件 1.进入项目根目录,打开终端: 2.输入 vi .gitignore 创建并打开隐藏文件.gitigno ...

  10. 自旋锁解决StackOverflowError案例

    本节笔者分享一个在实际工作中遇到的栈内存溢出(StackOverflowError)问题,以及其解决方案. 问题介绍:笔者负责的一个Java Web项目在启动的时候,需要有一些初始化操作,而接下来的代 ...