spider68

dijkstra algorithm

May 24th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. void dijkstra(vector<vector<int>> graph, int src, int V)
  2. {
  3.      queue<int>q;
  4.      q.push(src);
  5.      vector<int> dis(V, INT_MAX);
  6.      dis[src]=0;
  7.      while(!q.empty())
  8.      {
  9.          int u=q.front();
  10.          q.pop();
  11.          for(int j=0;j<V;j++)
  12.          {
  13.              if(graph[u][j]!=0&&dis[j]>dis[u]+graph[u][j])
  14.              {
  15.                  dis[j]=dis[u]+graph[u][j];
  16.                  q.push(j);
  17.              }
  18.          }
  19.      }
  20.      for(auto x:dis)cout<<x<<" ";
  21. }
Add Comment
Please, Sign In to add comment