HDU 1598 find the most comfortable road (最小生成树) >>

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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 105;
const int MAX = 0x3f3f3f3f;
const int mod = 1000000007;
int n, m, q, fa[205];
struct C {
    int v, w, num;
} ed[1005];
int Find(int x) {
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
bool cmp(C x, C y) {
    return x.num < y.num;
}
int main()
{
    while(~scanf("%d%d", &n, &m)) {
        for(int i = 0; i < m; i++) scanf("%d%d%d", &ed[i].v, &ed[i].w, &ed[i].num);
        sort(ed, ed + m, cmp);
        scanf("%d", &q);
        while(q--) {
            int st, en, ans = MAX;
            scanf("%d%d", &st, &en);
            for(int k = 0; k < m; k++) {
                for(int i = 1; i <= n; i++) fa[i] = i;
                for(int i = k; i < m; i++) {
                    int px = Find(ed[i].v), py = Find(ed[i].w);
                    if(px != py) fa[px] = py;
                    if(Find(st) == Find(en)) {
                        ans = min(ans, ed[i].num - ed[k].num);
                        break;
                    }
                }
            }
            if(ans == MAX) printf("-1\n");
            else printf("%d\n", ans);
        }
    }
    return 0;
}



HDU 1598 find the most comfortable road (最小生成树) >>

时间: 2024-08-25 21:28:36

HDU 1598 find the most comfortable road (最小生成树) >>的相关文章

HDU 1598 find the most comfortable road (最小生成树)

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5897 Accepted Submission(s): 2562 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结

hdu 1598 find the most comfortable road

http://acm.hdu.edu.cn/showproblem.php?pid=1598 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 2000 5 using namespace std; 6 const int inf=1<<30; 7 8 int f[maxn],n,m,q,a,b; 9 struct node 10 { 11 int u

hdu 1598 find the most comfortable road (并查集 + 枚举)

题目: 链接:点击打开链接 思路: 对边排序,再枚举每条边,如果出现通路(findset(x) == findset(y))就结束. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 220 #define MAXM 1010 #define MAX 100000000 stru

HDU 1598 find the most comfortable road (并查集||最短路)

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3720    Accepted Submission(s): 1583 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级

HDU 1598 find the most comfortable road (枚举+Kruskal) 最短路

Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的"舒适度"有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ), 但XX星人对时间却没那么多要求.要你找出一条城市间的最舒适的路径.(

hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3999    Accepted Submission(s): 1720 Problem Description XX 星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超

hdu 1598 find the most comfortable road 一开始把此题当做最短路。。后来发现不行。。并查集+枚举

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4350    Accepted Submission(s): 1876 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超

hdu 1598 find the most comfortable road(并查集+枚举)

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4899    Accepted Submission(s): 2131 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超

HDU 1598 find the most comfortable road (MST)

find the most comfortable road Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar