-
Notifications
You must be signed in to change notification settings - Fork 3
/
bus-routes.cpp
89 lines (74 loc) · 2 KB
/
bus-routes.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
//Runtime: 84 ms
class Solution {
public:
int binary(vector<int> a, int f){
int lo = 0;
int hi = a.size()-1;
while(lo <= hi){
int mid = lo + (hi-lo)/2;
if(a[mid] == f){
return mid;
}else if(a[mid] > f){
hi = mid -1;
}else{
lo = mid +1;
}
}
return -1;
}
bool isConnected(vector<int> a, vector<int> b){
int i = 0;
int j = 0;
while(i < a.size() && j < b.size()){
if(a[i] == b[j]){
return 1;
}
if(a[i] < b[j]) i++;
else j++;
}
return 0;
}
int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
vector<vector<int> >adj(routes.size());
if(S == T){
return 0;
}
for(auto a:routes){
sort(a.begin(), a.end());
}
for(int i=0;i<routes.size();i++){
for(int j=i+1;j<routes.size();j++){
if(isConnected(routes[i], routes[j])){
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
set<int> targetRoutes;
queue<pair<int, int> > q;
set<int> vis;
for(int i=0;i<routes.size();i++){
if(binary(routes[i], S) != -1){
q.push(make_pair(i, 1));
vis.insert(i);
}
if(binary(routes[i], T) != -1){
targetRoutes.insert(i);
}
}
while(!q.empty()){
pair<int, int> t = q.front();
q.pop();
if(targetRoutes.find(t.first) != targetRoutes.end()){
return t.second;
}
for(int a: adj[t.first]){
if(vis.find(a) == vis.end()){
vis.insert(a);
q.push(make_pair(a, t.second+1));
}
}
}
return -1;
}
};