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. Identity of indiscernibles(不可分与同一性)

    不可区分的对象或者实体,全部性质都相同.也就是说,实体 x 与实体 y是同一的,如果对 x 成立的全部断言,对 y 同样成立(反之亦然). 注意区分如下的两个概念: 同一性的不可分性(The indi ...

  2. 【3005】拦截导弹问题(noip1999)

    Time Limit: 3 second Memory Limit: 2 MB 某国为了防御帝国的导弹袭击,开发出一种导弹拦截系统,但是这种拦截系统有一个缺陷:虽然他的第一发炮弹能达到任意的高度,但是 ...

  3. Cygwin 与 MinGW/MSYS/MSYS2,如何选择?甚至还有GNU utilities for Win32

    Cygwin与MinGW/MSYS,如何选择? 2012-11-03更新:加入 MSYS 的内容. 2013-10-15更新:修改表格格式,加入介绍链接. 2014-12-17更新:加入 MSYS2 ...

  4. Redis主从高可用缓存

    nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存   一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集 ...

  5. HDU4876:ZCC loves cards

    Problem Description ZCC loves playing cards. He has n magical cards and each has a number on it. He ...

  6. [Grid Layout] Specify a grid gutter size with grid-gap

    It’s beautifully straightforward to add a gutter to our grid layout. Let’s apply one with grid-gap.

  7. [Angular] Updating and resetting FormGroups and FormControls

    If you want to reset or just update field value, you can do: reset: reset({key: value, k2:v2}); upda ...

  8. C#-numericUpDown-数字选择---ShinePans

    program.cs using System; using System.Collections.Generic; using System.Linq; using System.Windows.F ...

  9. nuklear(A single-header ANSI C gui library,界面还不错)

    Nuklear This is a minimal state immediate mode graphical user interface toolkit written in ANSI C an ...

  10. iis配置反向代理oss

    windows利用iis配置反向代理实现ECS内网互通oss IIS实现反向代理 新建两个站点,端口分别使用 80 和 81,在DNS中新建A记录,指向该计算机(10.4.34.41) 配置过程如下: ...