-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10048.cpp
163 lines (119 loc) · 2.29 KB
/
uva10048.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> ii;
#define TRvi(c, it) \
for(vector<ii>::iterator it = c.begin(); it != c.end(); it++)
#define BFS_WHITE -1
#define INF 2000000000
int V, E, Q;
vector<pair<int, ii> > EdgeList;
vector<vector<ii> > MST;
vector<int> pset;
int numDisjointSets;
void initSets(int num)
{
pset.assign(num, 0);
numDisjointSets = num;
for(int i = 0; i < num; i++)
pset[i] = i;
}
int findSet(int i)
{
if(pset[i] == i)
return i;
else
return pset[i] = findSet(pset[i]);
}
bool isSameSet(int i, int j)
{
return findSet(i) == findSet(j);
}
void unionSet(int i, int j)
{
if(!isSameSet(i, j))
numDisjointSets -= 1;
pset[findSet(i)] = findSet(j);
}
void Kruskal()
{
sort(EdgeList.begin(), EdgeList.end());
MST.clear();
MST.resize(V);
initSets(V);
for(int i = 0; i < EdgeList.size(); i++)
{
pair<int, ii> front = EdgeList[i];
int u = front.second.first, v = front.second.second;
int cost = front.first;
if(!isSameSet(u, v))
{
MST[u].push_back(ii(v, cost));
MST[v].push_back(ii(u, cost));
unionSet(u, v);
}
}
}
int bfs(int start, int destination)
{
vector<int> dist(V, -1);
queue<int> q;
q.push(start);
dist[start] = 0;
while(!q.empty())
{
int front = q.front();
q.pop();
if(front == destination)
break;
TRvi(MST[front], v)
{
if(dist[v->first] == -1)
{
dist[v->first] = max(dist[front], v->second);
q.push(v->first);
}
}
}
return dist[destination]; /* if it is -1 there is no path in MST */
}
int main()
{
int start, destination, cost;
int CaseNo = 0;
while(cin >> V >> E >> Q && V && E && Q)
{
CaseNo++;
EdgeList.clear();
for(int i = 0; i < E; i++)
{
cin >> start >> destination >> cost;
start--; destination--;
EdgeList.push_back(make_pair(cost, ii(start, destination)));
}
Kruskal();
if(CaseNo != 1)
cout << endl;
printf("Case #%d\n", CaseNo);
for(int q = 0; q < Q; q++)
{
cin >> start >> destination;
start--; destination--;
int minimax = bfs(start, destination);
if(minimax != -1)
cout << minimax << endl;
else
cout << "no path" << endl;
}
}
return 0;
}