1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define MAXN 210
  5. #define INF 2147483646
  6. using namespace std;
  7.  
  8. int f[MAXN], Rank[MAXN]; //Rank长度
  9. int n, m, pos;
  10.  
  11. struct Edge{
  12. int u, v, val;
  13. //按照val从小到大排列
  14. friend bool operator<(const Edge&a, const Edge&b){
  15. return a.val < b.val;
  16. }
  17. }arr[MAXN*MAXN];
  18.  
  19. void init(){
  20. for (int i = ; i<MAXN; ++i)
  21. f[i] = i, Rank[i] = ;
  22. }
  23.  
  24. int find(int x){
  25. int i, j = x;
  26. while (j != f[j]) j = f[j];
  27. while (x != j){
  28. i = f[x]; f[x] = j; x = i;
  29. }
  30. return j;
  31. }
  32.  
  33. void Union(int x, int y){
  34. int a = find(x), b = find(y);
  35. if (a == b)return;
  36. if (Rank[a]>Rank[b])
  37. f[b] = a;
  38. else{
  39. if (Rank[a] == Rank[b])
  40. ++Rank[b];
  41. f[a] = b;
  42. }
  43. }
  44.  
  45. int main(){
  46. int u, v, speed, Q;
  47. while (scanf("%d%d", &n, &m) != EOF){
  48. for (int i = ; i<m; ++i)
  49. scanf("%d%d%d", &arr[i].u, &arr[i].v, &arr[i].val);
  50. sort(arr, arr + m);
  51. scanf("%d", &Q);
  52. while (Q--){
  53. scanf("%d%d", &u, &v);
  54. int ans = INF;
  55. for (int j = ; j < m; ++j){
  56. init();
  57. for (int k = j; k < m; ++k){
  58. Union(arr[k].u, arr[k].v);
  59. if (find(u) == find(v)){
  60. ans = min(ans, arr[k].val - arr[j].val); //使ans尽可能的小
  61. break;
  62. }
  63. }
  64. }
  65. if (ans == INF)
  66. printf("-1\n");
  67. else
  68. printf("%d\n", ans);
  69. }
  70. }
  71. //system("pause");
  72. return ;
  73. }

hdu1598 find the most comfortable road 枚举+最小生成树的更多相关文章

  1. hdu1598 find the most comfortable road (枚举)+【并查集】

    <题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...

  2. HDU1598 find the most comfortable road 【并查集】+【枚举】

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

  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(枚举+并查集,类似于最小生成树)

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

  6. HDU 1598 find the most comfortable road (最小生成树) &gt;&gt;

    Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  7. [HDU1598]find the most comfortable road

    思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...

  8. hdu-1598 find the most comfortable road---kruskal+枚举下界

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Ro ...

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

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

随机推荐

  1. spring test---測试SpringMvc初识

    如今越来越多人使用SpringMvc来开发系统,在开发中可定须要对后台url地址请求測试,而且返回预期的结果! Spring提供的測试类MockMvc来进行url地址请求測试,使用方方式: packa ...

  2. sanic官方文档解析之Example(一)

    1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...

  3. SequenceFileInputFormat区别TextInputFormat

    通过InputFormat,Hadoop可以: l           检查MapReduce输入数据的正确性: l           将输入数据切分为逻辑块InputSplit,这些块会分配给Ma ...

  4. 常见mysql分布式数据中间件

    一般分为三种: 1. proxy sharding,目前由cobar,mycat,drds,atlas修改,这几个产品的起源一般是mysqlproxy 或 ameoba,特点是mysql协议基本兼容, ...

  5. SyntaxError:Strict mode does not allow function declaration in a lexically nested statement.

    问题描述   使用react-native init创建了一个新项目,在package.json中使用的react-native的版本如下: "dependencies": { & ...

  6. ICMP协议 广播以查询局域网内的所有主机

    看到了很多局域网内的主机扫描工具,在想怎么去实现这样一个工具.前几天看了Ping源码--ICMP协议的实例,ICMP可以用来探测网联网内的任一主机,ICMP和广播地址结合来扫描局域网内的所有主机不是很 ...

  7. extjs4.0 treepanel节点的选中、展开! 数据的重新加载

    1.extjs4.0API较3.0有非常大变化2.多级子父节点的选中和展开.3.数据的重新加载.tree.getStore().load4.节点的移除,从树中根据ID获取节点 tree.getStor ...

  8. Why you shouldn’t connect your mobile application to a database

    BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02   Working at Embarcadero, I frequently ...

  9. css3中animation的应用

    1.css3 的相关属性: 相关代码: div { animation-name: myfirst; //动画的名称 animation-duration: 5s; //动画一个周期需要5秒 anim ...

  10. CentOS7 安装和配置 mysql5.7

    1.下载 mysql源安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.安装mysql源 ...