_no0B

Untitled

Dec 11th, 2021
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. void dijkstra(int src , vector < int >& dis, int n)
  2. {
  3.     for(int i = 1 ; i <= n ; i++) dis[i] = MAX;
  4.     priority_queue < pair < int , int > > tempo;
  5.     tempo.push({0 , src});
  6.     while(!tempo.empty()){
  7.         pair < int , int > p = tempo.top();
  8.         tempo.pop();
  9.         int node = p.second;
  10.         if(-p.first > dis[node]) continue;
  11.         for(auto adj:edg[node]){
  12.             int a = adj.first , w = adj.second;
  13.             int val = dis[node] + w;
  14.             if(val < dis[a]){
  15.                 dis[a] = val;
  16.                 tempo.push({-val , a});
  17.             }
  18.         }
  19.     }
  20. } /// O(m * log(m) ) ; m = number of edges
Advertisement
Add Comment
Please, Sign In to add comment