标签: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