-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSecond Minimum Time to Reach Destination.cpp
60 lines (56 loc) · 1.82 KB
/
Second Minimum Time to Reach Destination.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
class Solution {
public:
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
// if the shortest path from 1 to n is of length L
// find whether there is a path of length L+1
// there is always a path of length L+2
vector<vector<int>> adj(n);
for (auto& e : edges) {
int u = e[0]-1, v = e[1]-1;
adj[u].push_back(v);
adj[v].push_back(u);
}
// bfs from goal
vector<int> d(n, 1e9);
d[n-1] = 0;
queue<int> q;
q.push(n-1);
while (!q.empty()) {
int cur = q.front(); q.pop();
for (auto nei : adj[cur]) {
if (d[nei] == 1e9) {
d[nei] = d[cur] + 1;
q.push(nei);
}
}
}
// check the existence of a path with length = d[0]+1
int len = d[0] + 2;
q.push(0);
bool done = false;
while (!q.empty()) {
int cur = q.front(); q.pop();
for (auto nei : adj[cur]) {
if (d[nei] == d[cur]) {
len--;
done = true;
break;
} else if (d[nei] == d[cur] - 1) {
q.push(nei);
}
}
if (done) break;
}
// calculate the time needed
// light : green in [0, c), [2c, 3c), ...
// red in [c, 2c), [3c, 4c), ...
int currTime = 0;
//cout << len << '\n';
for (int i = 0; i < len; i++) {
if ((currTime / change) % 2 == 1) // have to wait until the signal turns into green
currTime = ((currTime / change) + 1) * change;
currTime += time;
}
return currTime;
}
};