-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
82 lines (71 loc) · 2.47 KB
/
Solution.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
#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
// BFS for minimum path, remove all edges in the minimum path, then
// BFS again (until the new value is strictly larger)
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
vector<vector<int>> adj(n);
for (auto const& edge : edges) {
int from = edge[0];
int to = edge[1];
// convert to 0-index
adj[from - 1].push_back(to - 1);
adj[to - 1].push_back(from - 1); // bidirectional
}
// Store shortest distance/time to the node
vector<int> dist1(n, INT_MAX);
// Store second-shortest distance/ time to the node
vector<int> dist2(n, INT_MAX);
// {node, numberOfTimesSeen}
queue<pair<int, int>> frontier;
frontier.push({0, 1});
dist1[0] = 0;
while (!frontier.empty()) {
auto [node, freq] = frontier.front();
frontier.pop();
// If the node is seen for the first time, then the
// timeTaken is the shortest time to the node.
int timeTaken = freq == 1 ? dist1[node] : dist2[node];
// traffic signal is red at every odd number of `change` minutes
// 2i * change + change
if ((timeTaken / change) & 1) {
// If red, then wait till the next time where it is green
// can be difficult to understand, but its calculating the
// next interval/time when the signal is green.
timeTaken = ((timeTaken / change) + 1) * change;
}
// Add the time taken to traverse the edge
timeTaken += time;
for (int const& neighbour : adj[node]) {
if (dist1[neighbour] == INT_MAX) {
// First time seeing node, update shortest time
dist1[neighbour] = timeTaken;
frontier.push({neighbour, 1});
continue;
}
// check that the current path is not traversed before, by comparing
// dist1[neighbour] and timeTaken.
if (dist2[neighbour] == INT_MAX && dist1[neighbour] != timeTaken) {
// Check for early return
if (neighbour + 1 == n) {
return timeTaken;
}
dist2[neighbour] = timeTaken;
frontier.push({neighbour, 2});
continue;
}
// effectively ignores nodes that have been seen twice
}
}
return 0; // Only one node. 0 time taken
}
};