-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path815. Bus Routes.cpp
50 lines (49 loc) · 1.33 KB
/
815. 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
class Solution {
public:
int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
if (S == T) {
return 0;
}
unordered_map<int, vector<int>>m(501); // Stop to Bus
int n = routes.size();
// Build graph Stop-to-Bus
for (int i = 0; i < n; ++i) {
for (int j = 0; j < routes[i].size(); ++j) {
m[routes[i][j]].push_back(i);
}
}
vector<int>visitedBus(501);
vector<int>visitedStop(1000001);
// BFS
queue<int>cur;
queue<int>next;
for (int& b: m[S]) {
cur.push(b);
}
int count = 1;
while (!cur.empty()) {
int bus = cur.front();
cur.pop();
visitedBus[bus] = 1;
for (int& s: routes[bus]) {
if (s == T) {
return count;
}
if (visitedStop[s]) {
continue;
}
visitedStop[s] = 1;
for (int& b: m[s]) {
if (!visitedBus[b]) {
next.push(b);
}
}
}
if (cur.empty()) {
++count;
swap(cur, next);
}
}
return -1;
}
};