File size: 2,148 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>

using namespace std;

struct UnionFind{
   vector <int> path, rank;

   UnionFind(int N_){
      rank.assign(N_, 0);
      path.assign(N_, 0);
      for(int it = 0; it < N_; it++) path[it] = it;
   };

   int findSet(int idx){
      return ( path[idx] == idx ? idx : path[idx] = findSet(path[idx]) );
   }

   bool isSameSet(int idx1, int idx2){
      return ( findSet(idx1) == findSet(idx2) );
   }

   void unionSet(int idx1, int idx2){
      if( isSameSet(idx1, idx2) ) return;
      int x = findSet(idx1), y = findSet(idx2);
      if( rank[x] > rank[y] ) path[y] = x;
      else path[x] = y;
      if( rank[x] == rank[y] ) rank[y]++;
   }
};

const int LIM = 110, SIZE = 10000;

typedef pair <int, int> ii;

struct Edge_{
   int u, v, w;
   bool operator < (const Edge_& other) const{
      return ( w < other.w );
   }
}E[SIZE];

int tc, N, M, Q, S, D, edge;
vector < ii > AdjList[LIM];
bool vis[LIM];

void dfs(int u, int v, int w){
   vis[u] = true;
   if( u == v ){
      edge = w;
      return;
   }
   for(int it = 0; it < AdjList[u].size(); it++)
      if( not vis[AdjList[u][it].first] )
         dfs(AdjList[u][it].first, v, max(w, AdjList[u][it].second) );
}

void Kruskal(){
   UnionFind UF(N + 1);
   int setSize = 1;
   sort(E, E + M);
   for(int it = 0; it < M and setSize != N; it++)
      if( not UF.isSameSet(E[it].u, E[it].v) ){
         AdjList[E[it].u].push_back( ii(E[it].v, E[it].w) );
         AdjList[E[it].v].push_back( ii(E[it].u, E[it].w) );
         setSize++, UF.unionSet(E[it].u, E[it].v);
      }
}

void clear_(){
   for(int it = 1; it <= N; it++) AdjList[it].clear();
}

int main(){
   while(scanf("%d %d %d", &N, &M, &Q), N | M | Q){
      if(tc++) putchar('\n');
      for(int it = 0; it < M; it++)
         scanf("%d %d %d", &E[it].u, &E[it].v, &E[it].w);
      Kruskal();
      printf("Case #%d\n", tc);
      for(int it = 0; it < Q; it++){
         scanf("%d %d", &S, &D);
         fill(vis, vis + N + 1, false);
         edge = -1;
         dfs(S, D, 0);
         if( edge == -1 ) puts("no path");
         else printf("%d\n", edge);
      }
      clear_();
   }
   return(0);
}