zoukankan      html  css  js  c++  java
  • hdu 1598 find the most comfortable road

    题意:给出一幅无向图,每次询问给出起点和终点,求起点到终点权值差最小的一条路,输出权值差;

    思路:边按权值排序,利用并查集枚举每次能使起点终点联通的情况下权值差,并每次更新最小值;

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define INF 0x3f3f3f3f
    int fa[500010],n,m;
    struct node
    {
        int u,v,val;
        friend bool operator<(const node &a,const node &b)
        {
            return a.val<b.val;
        }
    }arr[500010];
    void init()
    {
        for(int i=0;i<n;i++)
        {
            fa[i]=i;
        }
    }
    int fin(int x)
    {
        if(fa[x]==x) return x;
        return fa[x]=fin(fa[x]);
    }
    void combine(int a,int b)
    {
        int t1=fin(a);
        int t2=fin(b);
        if(t1!=t2)
            fa[t2]=t1;
    }
    int main()
    {
        int i,j,k,a,b,q,ans,flag;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(i=0;i<m;i++)
                scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].val);
            sort(arr,arr+m);//按速度从小到大排序
            scanf("%d",&q);
            for(i=0;i<q;i++)
            {
                scanf("%d%d",&a,&b);
                ans=INF;
                for(j=0;j<m;j++)//枚举恰能使两点联通的边数
                {
                    init();
                    for(k=j;k>=0;k--)
                    {
                        combine(arr[k].u,arr[k].v);
                        if(fin(a)==fin(b))
                        {
                            ans=min(ans,arr[j].val-arr[k].val);//联通时,求最小差值
                        }
                    }
                }
                if(ans==INF) printf("-1
    ");
                else printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    Matplotlib学习
    Docker win10安装
    pandas读取文件报错
    python特殊函数__str__、__repr__和__len__
    tar命令总结
    lamp服务器站点目录被植入广告代码处理
    linux简单测试
    中国剩余定理
    牛客暑期第六场G /// 树形DP 最大流最小割定理
    逆元 组合A(n,m) C(n,m)递推 隔板法
  • 原文地址:https://www.cnblogs.com/dashuzhilin/p/4418271.html
Copyright © 2011-2022 走看看