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

  1. 4 4
  2. 1 2 2
  3. 2 3 4
  4. 1 4 1
  5. 3 4 2
  6. 2
  7. 1 3
  8. 1 2

Sample Output

  1. 1
  2. 0

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<algorithm>
  4. using namespace std;
  5. const int maxn=1000+5;
  6. struct node{
  7. int st;
  8. int ed;
  9. int fu;
  10. }road[maxn];
  11. int p[maxn];
  12. int st,ed;
  13. int miin;
  14. int inf=1000000+5;
  15. bool cmp(node x,node y)
  16. {
  17. return x.fu<y.fu;
  18. }
  19. int find(int x)
  20. {
  21. return p[x]== x ?
  22.  
  23. x:p[x]=find(p[x]);
  24. }
  25.  
  26. int main()
  27. {
  28. int n,m;
  29. while(cin>>n>>m)
  30. {
  31. for(int i=1;i<=m;i++)
  32. scanf("%d%d%d",&road[i].st,&road[i].ed,&road[i].fu);
  33. sort(road+1,road+m+1,cmp);
  34. int tot;
  35. cin>>tot;
  36. while(tot--)
  37. {
  38. miin=inf;
  39. scanf("%d%d",&st,&ed);
  40. for(int i=1;i<=m;i++)
  41. {
  42. for(int i=1;i<=n;i++) p[i]=i;
  43. for(int j=i;j<=m;j++)
  44. {
  45. int x=find(road[j].st);
  46. int y=find(road[j].ed);
  47. if(x!=y) p[x]=y;
  48. if(find(st)==find(ed))
  49. {
  50. int sum=road[j].fu-road[i].fu;
  51. if(sum<miin) miin=sum;
  52. break;
  53. }
  54. }
  55. }
  56. if(miin==inf)
  57. printf("-1\n");
  58. else
  59. printf("%d\n",miin);
  60. }
  61.  
  62. }
  63. return 0;
  64. }

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

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. eclipse插件安装验证及问题处理

    eclipse插件安装验异常时可看当前workspace下面的.metadata/.log文件,找到具体的问题来处理.一般常用到插件安装不成功的原因如下: 1.jar包冲突: 2.jar包依赖的jav ...

  2. Eclipse查看某个方法被哪些类调用

    方法一:打开该类,在类的定义上即类名上,右键-->References--->Project ,就可以查看该类是否被工程中的其他Java文件引用过:但是如果在JSP页面,这个方法查不出来 ...

  3. Engine工具栏按钮的使用详解

    转自原文 Engine自定义控件实现toolbar功能 Engine提供的工具条能够轻易实现各种操作,非常方便,可是不好的地方就是太死板了,toolbar的图标都不能改.因此需要自己做按钮做控件去实现 ...

  4. POJ 2185 Milking Grid KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  5. 命令行运行python模块时提示包找不到的问题

    庄稼人不是专职python开发的道友.尽管与python相识已多年,可惜相识不相知,仅仅是偶尔借助pydev写一些简单的小工具. 多年来.一直困惑于这样一个问题:相同的project.相同的代码,使用 ...

  6. C#+HtmlAgilityPack

    C#+HtmlAgilityPack—糗事百科桌面版V2.0   最近在浏览以前自己上传的源码,发现在糗事百科桌面端源码评论区中,有人说现在程序不能用了.查看了一下源码运行情况,发现是正则表达式解析问 ...

  7. 复制相关参数学习笔记--slave上的参数

    server_id server_uuid   relay_log io_thread 读取过来的本地日志. relaylog文件名前缀,可以是全路径.   relay_log_index relay ...

  8. 【转】优先队列priority_queue 用法详解

    http://www.cnblogs.com/void/archive/2012/02/01/2335224.html 优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的 ...

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

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

  10. [React Router v4] Conditionally Render a Route with the Switch Component

    We often want to render a Route conditionally within our application. In React Router v4, the Route ...