cpp
复制代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
const int N = 1e3 + 5, M = 1e5 + 5;
int n, m, k;
struct edge {
int a, b, w;
}edge[M];
int fa[N],d[N],vis[N];
vector<pair<int, int>>G[N];
int find(int x) {
if (fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
int cmp(const struct edge& a, const struct edge& b) {
return a.w < b.w;
}
void spfa(int x) {
queue<int>q;
memset(d, 0x3f3f3f3f, sizeof(d));
memset(vis, 0, sizeof(vis));
d[x] = 0;
q.push(x);
int t;
while (!q.empty()) {
t = q.front();
q.pop();
vis[x] = 0;
for (int i = 0,j,w; i < G[t].size(); i++) {
j = G[t][i].first, w = G[t][i].second;
if (d[j] > max(d[t],w)) {
d[j] = max(d[t], w);
if (!vis[j]) {
q.push(j);
vis[j] = 1;
}
}
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].w);
}
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
sort(edge + 1, edge + 1 + m, cmp);
int x, y;
for (int i = 1; i <= m; i++) {
x = find(edge[i].a);
y = find(edge[i].b);
if (y != x) {
G[edge[i].a].push_back({ edge[i].b,edge[i].w });
G[edge[i].b].push_back({ edge[i].a,edge[i].w });
fa[x] = y;
}
}
for (int i = 1; i <= k; i++) {
scanf("%d%d", &y, &x);
spfa(y);
if (d[x] == 0x3f3f3f3f)
printf("-1\n");
else {
printf("%d\n", d[x]);
}
}
return 0;
}