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

struct node{
    int s,e,w;
}edge[MAXM];

int root[MAXN];
int n,m,q;

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

int findset(int x)
{
    return root[x] == x ? x : root[x] = findset(root[x]);
}

void mergeset(int x,int y)
{
    root[findset(x)] = findset(y);
}

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

int main()
{
    //freopen("input.txt","r",stdin);
    int x,y;
    int minn;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        int i,j;
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].w);
        }
        sort(edge,edge+m,cmp);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&x,&y);
            minn = MAX;
            for(i=0; i<m; i++)
            {
                init();
                for(j=i; j<m; j++)
                {
                    mergeset(edge[j].s,edge[j].e);
                    if(findset(x) == findset(y))
                        break;
                }
                if(j == m)
                    break;
                if(minn > edge[j].w - edge[i].w)
                    minn = edge[j].w - edge[i].w;
            }
            if(minn == MAX)
                printf("-1\n");
            else
                printf("%d\n",minn);
        }
    }
    return 0;
}

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

时间: 2024-10-07 17:49:08

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): 3720    Accepted Submission(s): 1583 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级

hdu1598find the most comfortable road(并查集+枚举,求起点到终点的边中最大边减最小边差值最小)

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

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 (枚举+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): 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): 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): 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): 4899    Accepted Submission(s): 2131 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超