Skip to content

Commit

Permalink
Create 2045. Second Minimum Time to Reach Destination (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Jul 28, 2024
2 parents 52e862d + 98ca850 commit 8e7c7fa
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2045. Second Minimum Time to Reach Destination
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
public:
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
vector<vector<int>>adj(n+1);
for(auto it : edges) {
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
vector<int>mn(n+1,1e9), smn(n+1,1e4);

queue<pair<int,int>>q;
mn[1] = 0;
q.push({1,0});
while(q.size()){
int node = q.front().first;
int wt = q.front().second;
q.pop();
for(auto it : adj[node]){
if(mn[it] > wt+1){
mn[it] = wt+1;
q.push({it, wt+1});
}

}
}
q.push({1,0});
while(q.size()){
int node = q.front().first;
int wt = q.front().second;
q.pop();
for(auto it : adj[node]){

if(wt+1>mn[it] && wt+1<smn[it]){
smn[it] = wt+1;
q.push({it, wt+1});

}
else if(wt+1 == mn[it]) q.push({it, wt+1});

}
}
int ans = 0;
while(smn[n]--){
ans += time;
cout<<ans<<" ";
if((ans / change)%2 && smn[n]){
ans += (change - ans%change);
cout<<ans<<" , ";
}
}
return ans;
}
};

0 comments on commit 8e7c7fa

Please sign in to comment.