#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 210
#define INF 2147483646
using namespace std; int f[MAXN], Rank[MAXN]; //Rank长度
int n, m, pos; struct Edge{
int u, v, val;
//按照val从小到大排列
friend bool operator<(const Edge&a, const Edge&b){
return a.val < b.val;
}
}arr[MAXN*MAXN]; void init(){
for (int i = ; i<MAXN; ++i)
f[i] = i, Rank[i] = ;
} int find(int x){
int i, j = x;
while (j != f[j]) j = f[j];
while (x != j){
i = f[x]; f[x] = j; x = i;
}
return j;
} void Union(int x, int y){
int a = find(x), b = find(y);
if (a == b)return;
if (Rank[a]>Rank[b])
f[b] = a;
else{
if (Rank[a] == Rank[b])
++Rank[b];
f[a] = b;
}
} int main(){
int u, v, speed, Q;
while (scanf("%d%d", &n, &m) != EOF){
for (int i = ; i<m; ++i)
scanf("%d%d%d", &arr[i].u, &arr[i].v, &arr[i].val);
sort(arr, arr + m);
scanf("%d", &Q);
while (Q--){
scanf("%d%d", &u, &v);
int ans = INF;
for (int j = ; j < m; ++j){
init();
for (int k = j; k < m; ++k){
Union(arr[k].u, arr[k].v);
if (find(u) == find(v)){
ans = min(ans, arr[k].val - arr[j].val); //使ans尽可能的小
break;
}
}
}
if (ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
}
}
//system("pause");
return ;
}

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. [转载]CodeIgniter配置之URL

    应该有很多项目中会有这样的情况,通过 http://pc.local 可以访问,若通过 http://localhost/pc/public 则会出现一些图片.样式显示不到,超链接出错的情况,项目的移 ...

  2. zoj1232Adventure of Super Mario(图上dp)

    题目连接: 啊哈哈.点我点我 思路: 这个题目是一个图上dp问题.先floyd预处理出图上全部点的最短路,可是在floyd的时候,把可以用神器的地方预处理出来,也就是转折点地方不能为城堡..预处理完成 ...

  3. openwrt gstreamer实例学习笔记(一.初始化gstreamer)

    GStreamer 是一个非常强大而且通用的流媒体应用程序框架. GStreamer所具备的很多优点来源于其框架的模块化: GStreamer能够无缝的合并新的插件. 但是, 由于追求模块化和高效率, ...

  4. Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

    F. Group Projects   There are n students in a class working on group projects. The students will div ...

  5. Scaling with Microservices and Vertical Decomposition

    Scaling with Microservices and Vertical Decomposition – dev.otto.de https://dev.otto.de/2014/07/29/s ...

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

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

  7. 解析SQL中的包含的列和表

    using System; using System.IO; using System.Collections.Generic; namespace SQLProcess { class Progra ...

  8. id、NSObject *、id<NSObject>、instancetype

    1. id 与 NSObject * (1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的 ...

  9. i2c_set_clientdata函数【转】

    本文转载自‘:http://blog.csdn.net/jk198310/article/details/43738367 在i2c驱动中有很多函数和数据结构,很多一时难以理解,所以写下本文共同学习. ...

  10. 有关定时器setTimeout()、setInterval()详解

    JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成. setTimeout() setTimeout函数用 ...