zoukankan      html  css  js  c++  java
  • hdu 1598

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define N 1005
    #define INF 0x1f1f1f1f
    struct tt{
        int x,y,sp;
    }p[N];
    int pre[N];
    bool cmp(tt a, tt b)
    {
        return a.sp<b.sp;
    }
    int find(int x)
    {
        int r = x;
        while(x!=pre[x]) x = pre[x];
        while(r!=x)
        {
            int j = pre[r];
            pre[r] = x;
            r = j;
        }
        return x;
    }
    int main()
    {
        int n,q,i,j,s,t,m;
        while(~scanf("%d %d",&n,&m))
        {
            for(i = 0 ; i < m ; i++)
                scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].sp);
            sort(p,p+m,cmp);
    
            scanf("%d",&q);
            while(q--)
            {   int minx = INF;
                scanf("%d %d",&s,&t);
                for(i = 0 ; i < m ; i++)
                {
                    for(j = 0 ; j <= n ; j++) pre[j] = j;
                    for(j = i ; j < m ; j++)
                    {
                        int fx = find(p[j].x);
                        int fy = find(p[j].y);
                        if(fx!=fy) pre[fx] = fy;
                        if(find(s) == find(t)) minx = min(minx,p[j].sp - p[i].sp);
                    }
                }
                if(minx!=INF) printf("%d
    ",minx);
                else printf("-1
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    很难理解的三个设计模式
    设计模式思考(转)
    AOP
    CAP理论(摘)
    DDBS
    NoSql
    Enterprise Library 企业库
    padright padleft
    Process ProcessThread Thread
    053374
  • 原文地址:https://www.cnblogs.com/llei1573/p/3912934.html
Copyright © 2011-2022 走看看