-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10048 - Audiophobia.cpp
executable file
·76 lines (64 loc) · 1.35 KB
/
10048 - Audiophobia.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
#include<bits/stdc++.h>
#define MAX 10005
using namespace std;
vector<int>v(MAX);
int find(int x){
return x==v[x]? x:find(v[x]);
}
void Union(int a, int b){
v[find(a)] = find(b);
}
struct Edge{
int x;
int y;
int d;
};
bool cmp(Edge a, Edge b){
return a.d<b.d;
}
int main(){
int c,s,q,cases=0;
while(cin>>c>>s>>q){
if(c==0&&s==0&&q==0)return 0;
if(cases>0)cout<<endl;
cases++;
cout<<"Case #"<<cases<<endl;
for(int i=1;i<=c;++i)v[i] = i;
vector<Edge> paths(s);
for(int i = 0;i<s;++i)cin>>paths[i].x>>paths[i].y>>paths[i].d;
sort(paths.begin(),paths.end(),cmp);
vector<Edge>MST;
vector<vector<int> >ady(c+1,vector<int>(c+1));
for(int i = 0;i<s&&(int)MST.size()<c-1;++i){
if(find(paths[i].x)!=find(paths[i].y)){
Union(paths[i].x,paths[i].y);
MST.push_back(paths[i]);
ady[paths[i].x][paths[i].y] = paths[i].d;
ady[paths[i].y][paths[i].x] = paths[i].d;
}
}
int a,b,rpta;
while(q--){
rpta = -1;
cin>>a>>b;
//bfs
vector<int>dist(c+1,-1);
dist[a] = 0;
queue<int> Q;
Q.push(a);
while(!Q.empty()){
int aux = Q.front();
Q.pop();
if(aux==b){
rpta = dist[b];
break;
}
for(int i =1 ;i<=c;++i){
if(ady[aux][i]!=0&&dist[i]==-1)Q.push(i),dist[i] = max(dist[aux] , ady[aux][i]) ;
}
}
if(rpta>0)cout<<rpta<<endl;
else cout<<"no path"<<endl;
}
}
}