-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusroutes.cpp
38 lines (37 loc) · 1.21 KB
/
busroutes.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
class Solution {
public:
int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
int i,j;
pair<int,int> pred;
queue<pair<int,int> > Q;
unordered_map<int,bool> visited;
unordered_map<int,unordered_map<int,int> > bus2stop, stop2bus;
if (S == T)
return 0;
for (i = 0; i < routes.size(); i++) {
for (j = 0; j < routes[i].size(); j++) {
bus2stop[i][routes[i][j]]++;
stop2bus[routes[i][j]][i]++;
}
}
for (auto &x : stop2bus[S]) {
Q.push({x.first,1});
visited[x.first] = true;
}
while (!Q.empty()) {
pred = Q.front();
Q.pop();
if (bus2stop[pred.first].find(T) != bus2stop[pred.first].end())
return pred.second;
for (auto &stop : bus2stop[pred.first]) {
for (auto &bus : stop2bus[stop.first]) {
if (!visited[bus.first]) {
Q.push({bus.first,pred.second+1});
visited[bus.first] = true;
}
}
}
}
return -1;
}
};