码迷,mamicode.com
首页 > 其他好文 > 详细

极品飞车

时间:2019-03-31 13:35:46      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:printf   oid   algorithm   pac   using   std   turn   return   --   

#include <cstdio>
#include <cstring>
#include <algorithm>
#define rgi register int
#define il inline
#define ll long long
#define maxn 10000
#define maxm 10000
#define inf 2147483647

using namespace std;

int n, m, q;
int p[maxn];

struct edge
{
    int u, v, w;
} e[maxm];

il bool cmp(struct edge a, struct edge b)
{
    return a.w < b.w;
}

il void init()
{
    for(rgi i = 1; i <= n; ++i)
        p[i] = i;
}

il int find(int x)
{
    return x == p[x] ? x : p[x] = find(p[x]);
}

il int _union(int a, int b)
{
    p[find(b)] = find(a);
}

il int judge(int a, int b)
{
    return find(a) == find(b);
}

int main()
{
    scanf("%d %d", &n, &m);
    for(rgi i = 1; i <= m; ++i)
        scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
    sort(e + 1, e + n + 1, cmp);
    scanf("%d", &q);
    while(q--)
    {
        int s, t, delta = inf;
        scanf("%d %d", &s, &t);
        for(rgi i = 1; i <= m; ++i)
        {
            init();
            for(rgi j = i; j <= m; ++j)
            {
                _union(e[j].u, e[j].v);
                if(judge(s, t))
                {
                    delta = min(delta, e[j].w - e[i].w);
                    break;
                }
            }
        }
        if(delta == inf)
            printf("-1\n");
        else
            printf("%d\n", delta);
    }
    return 0;
}

极品飞车

标签:printf   oid   algorithm   pac   using   std   turn   return   --   

原文地址:https://www.cnblogs.com/Clever-Jimmy/p/10630855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!