File tree 1 file changed +40
-0
lines changed
solution/2000-2099/2045.Second Minimum Time to Reach Destination
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < array>
2
+ #include < climits>
3
+ #include < queue>
4
+ #include < vector>
5
+ using namespace std ;
6
+
7
+ class Solution {
8
+ public:
9
+ int secondMinimum (int n, vector<vector<int >>& edges, int time, int change) {
10
+ vector<vector<int >> adj (n + 1 );
11
+ for (auto & e : edges) {
12
+ adj[e[0 ]].push_back (e[1 ]);
13
+ adj[e[1 ]].push_back (e[0 ]);
14
+ }
15
+ vector<array<int , 2 >> dist (n + 1 , {INT_MAX, INT_MAX});
16
+ queue<pair<int , int >> q;
17
+ dist[1 ][0 ] = 0 ;
18
+ q.emplace (1 , 0 );
19
+
20
+ while (!q.empty ()) {
21
+ auto [u, t] = q.front ();
22
+ q.pop ();
23
+ for (int v : adj[u]) {
24
+ int cycles = t / change;
25
+ int wait = (cycles % 2 == 1 ? change - (t % change) : 0 );
26
+ int t2 = t + wait + time ;
27
+ if (t2 < dist[v][0 ]) {
28
+ dist[v][1 ] = dist[v][0 ];
29
+ dist[v][0 ] = t2;
30
+ q.emplace (v, t2);
31
+ } else if (t2 > dist[v][0 ] && t2 < dist[v][1 ]) {
32
+ dist[v][1 ] = t2;
33
+ q.emplace (v, t2);
34
+ }
35
+ }
36
+ }
37
+
38
+ return dist[n][1 ];
39
+ }
40
+ };
You can’t perform that action at this time.
0 commit comments