标签:
题目要求是可达情况下,舒适度最好,即速度之差最小。
1.因为不考虑距离 所以只要在同一集合中即可
2.需要在条件1下枚举最小速度,然后找符合条件的
1 #include "iostream" 2 #include "algorithm" 3 #include "memory.h" 4 #include "cmath" 5 #include "string" 6 #include "vector" 7 #include "algorithm" 8 #include "climits" 9 using namespace std; 10 #define MAXN 1111 11 #define _min(a,b) (((a)<(b))?(a):(b)) 12 struct node { 13 int x, y,speed; 14 bool operator < (node a) const{ 15 return speed < a.speed; 16 } 17 }p[MAXN]; 18 int fa[MAXN]; 19 int n, m, q, t; 20 int re, num, ans; 21 22 int find(int x) 23 { 24 if (fa[x] < 0) return x; 25 return fa[x] = find(fa[x]); 26 } 27 28 void merge(int a, int b) 29 { 30 int x = find(a); 31 int y = find(b); 32 if (x < y) { 33 fa[x] += fa[y]; 34 fa[y] = x; 35 } 36 if (x > y) { 37 fa[y] += fa[x]; 38 fa[x] = y; 39 } 40 } 41 42 43 int main() 44 { 45 ios::sync_with_stdio(false); 46 while (cin >> n >> m) { 47 for (int i = 0; i < m; ++ i) { 48 cin >> p[i].x >> p[i].y >> p[i].speed; 49 } 50 sort(p,p+m); 51 cin >> q; 52 for (int i = 0; i < q; ++i) { 53 int u, v; 54 cin >> u >> v; 55 ans = INT_MAX; 56 for (int j = 0; j < m; ++j) { 57 memset(fa,-1,sizeof(fa)); 58 for (int k = j; k < m; ++k) { 59 merge(p[k].x,p[k].y); 60 if (find(u) == find(v)) { 61 ans = _min(ans, p[k].speed - p[j].speed); 62 break; 63 } 64 } 65 } 66 if (ans == INT_MAX) cout << "-1" << endl; 67 else cout << ans << endl; 68 } 69 } 70 return 0; 71 }
hdu 1598 find the most comfortable road
标签:
原文地址:http://www.cnblogs.com/usedrosee/p/4248460.html