POJ 1598 find the most comfortable road

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598

思路:通过思路转换,可以看出是求两个点之间最大边权值与最小边权值之差最小的。克鲁斯卡尔算法枚举之~

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
int n,m;
int f[210];

struct node
{
    int u,v;
    int w;
}map[1020];

int getf(int a)
{
    if(f[a] == a)
        return a;
    f[a] = getf(f[a]);
    return f[a];
}

void merge(int a, int b)
{
    int x = getf(a);
    int y = getf(b);

    if(x!=y)
        f[y] = x;
}

void init()
{
    for(int i=1; i<=n; i++)
    {
        f[i] = i;
    }
}

bool cmp(node a, node b)
{
    return a.w < b.w;
}

int solve(int start, int end)
{
    int maxn = -1;
    int minn = 999999999;
    sort(map,map+m,cmp);
    int res = 999999999;

    for(int k = 0; k < m; k++)
    {
        init();
        maxn = -1;
        minn = 999999999;
        for(int i = k; i < m; i++)
        {
            if(getf(map[i].u) != getf(map[i].v))
            {
                merge(map[i].u, map[i].v);
                maxn = max(maxn, map[i].w);
                minn = min(minn, map[i].w);
            }
            if(getf(start) == getf(end))
            {
                res = min(maxn - minn, res);
                break;
            }
        }
    }

    if(res == 999999999)
        res = -1;
    return res;
}

int main()
{
    int q;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d", &map[i].u, &map[i].v, &map[i].w);
        }

        scanf("%d", &q);
        int s[20][2];

        for(int i=1; i<=q; i++)
        {
            init();
            scanf("%d %d", &s[i][0], &s[i][1]);
            printf("%d\n", solve(s[i][0],s[i][1]));
        }
    }
    return 0;
}
时间: 2024-10-13 11:57:12

POJ 1598 find the most comfortable road的相关文章

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 (最小生成树) &gt;&gt;

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

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星人对时间却没那么多要求.要你找出一条城市间的最舒适的路径.(

hdoj 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): 4022    Accepted Submission(s): 1732 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): 5897 Accepted Submission(s): 2562 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