Skip to content

Commit 8e7c7fa

Browse files
authored
Create 2045. Second Minimum Time to Reach Destination (#540)
2 parents 52e862d + 98ca850 commit 8e7c7fa

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public:
3+
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
4+
vector<vector<int>>adj(n+1);
5+
for(auto it : edges) {
6+
adj[it[0]].push_back(it[1]);
7+
adj[it[1]].push_back(it[0]);
8+
}
9+
vector<int>mn(n+1,1e9), smn(n+1,1e4);
10+
11+
queue<pair<int,int>>q;
12+
mn[1] = 0;
13+
q.push({1,0});
14+
while(q.size()){
15+
int node = q.front().first;
16+
int wt = q.front().second;
17+
q.pop();
18+
for(auto it : adj[node]){
19+
if(mn[it] > wt+1){
20+
mn[it] = wt+1;
21+
q.push({it, wt+1});
22+
}
23+
24+
}
25+
}
26+
q.push({1,0});
27+
while(q.size()){
28+
int node = q.front().first;
29+
int wt = q.front().second;
30+
q.pop();
31+
for(auto it : adj[node]){
32+
33+
if(wt+1>mn[it] && wt+1<smn[it]){
34+
smn[it] = wt+1;
35+
q.push({it, wt+1});
36+
37+
}
38+
else if(wt+1 == mn[it]) q.push({it, wt+1});
39+
40+
}
41+
}
42+
int ans = 0;
43+
while(smn[n]--){
44+
ans += time;
45+
cout<<ans<<" ";
46+
if((ans / change)%2 && smn[n]){
47+
ans += (change - ans%change);
48+
cout<<ans<<" , ";
49+
}
50+
}
51+
return ans;
52+
}
53+
};

0 commit comments

Comments
 (0)