Skip to content

Instantly share code, notes, and snippets.

@praneethreddymallupally
Created July 18, 2020 10:34
Show Gist options
  • Save praneethreddymallupally/52dc9c660e9c60d624977cdcc3acbff5 to your computer and use it in GitHub Desktop.
Save praneethreddymallupally/52dc9c660e9c60d624977cdcc3acbff5 to your computer and use it in GitHub Desktop.
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int N, int K) {
const int E=times.size();
int time=-1;
vector<pair<int,int>> adj[N+1];
for(int i=0;i<E;i++){
adj[times[i][0]].push_back(make_pair(times[i][1],times[i][2]));
}
vector<int> d(N+1,1<<30);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push(make_pair(0,K));
d[K]=0;
while(!pq.empty()){
int u=pq.top().second;
int dorikindidonga=pq.top().first;
pq.pop();
if(d[u]>=dorikindidonga){
time=max(dorikindidonga,time);
for(auto x:adj[u]){
int v=x.first;
int weight=x.second;
if(d[v]>d[u]+weight){
d[v]=d[u]+weight;
pq.push(make_pair(d[v],v));
}
}
}
}
for(int i=1;i<=N;i++){
if(d[i]==1<<30){
return -1;
}
}
return time;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment