-
Notifications
You must be signed in to change notification settings - Fork 0
/
itineraries_v1.cpp
59 lines (47 loc) · 1.25 KB
/
itineraries_v1.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
#include <iostream>
#include <vector>
#include <queue>
#include "MST.h"
using namespace std;
const int N = 2e5+5;
const int M = 3e5+5;
const int L = 5e5+5;
int n, m, l;
vector<pair<int,int>> adjList[N];
// compute max noise level (BFS approach)
int query(int u, int v){
vector<bool> vis(n);
queue <pair<int,int>> q;
// start at u with zero max_noise
q.push({u,0});
while(!q.empty()){
int cur = q.front().first;
int max_noise = q.front().second;
q.pop();
vis[cur] = true;
// test if v was reached
if(cur == v) return max_noise;
// iterate through childs of cur vertex
for(auto [child, noise] : adjList[cur]){
// if child not visited, add to queue and update maximum seen noise
if(!vis[child]) q.push({child, max(max_noise, noise)});
}
}
return -1; // v unreachable from u (expected to never happen)
}
int main(){
vector<Edge> edgeList;
cin >> n >> m;
while(m--){
int u, v, c;
cin >> u >> v >> c;
edgeList.push_back(Edge(u-1, v-1, c));
}
build_MST(adjList, edgeList, n);
cin >> l;
while(l--){
int u, v;
cin >> u >> v;
cout << query(u-1, v-1) << endl;
}
}