Advertisement
akashtadwai

Dijkstra

Aug 19th, 2021
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. vector<int> dijkstra(int v,int n ,vector<vector<array<int,2>>>&g){
  2.     vector<int>dis(n,INF);
  3.     dis[v]=0;
  4.     priority_queue<array<int,2>,vector<array<int,2>>,greater<array<int,2>> >pq;
  5.     pq.push({0,v});
  6.     while(!pq.empty()){
  7.         int ver=pq.top()[1],dist=pq.top()[0];
  8.         pq.pop();
  9.         if(dis[ver]!=dist) continue;
  10.         for(auto u:g[ver]){
  11.             int to = u[0], weight = u[1];
  12.             if(dis[to]>dis[ver]+weight){
  13.                 dis[to]=dis[ver]+weight;
  14.                 pq.push({dis[to],to});
  15.             }
  16.         }
  17.     }
  18.     return dis;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement