Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.74 KB | None | 0 0
  1. auto dijkstra(int[][int][int] graph, int start)
  2. {
  3.     auto used = new int[graph.length];
  4.     auto path = new int[graph.length];
  5.     path[] = int.max; path[start] = 0;
  6.  
  7.     auto heapRange = new int[graph.length];
  8.     auto heap = heapify!((a, b) => path[a] > path[b])(heapRange, 0);
  9.     heap.insert(start);
  10.  
  11.     while (!heap.empty)
  12.     {
  13.         auto currentVertex = heap.removeAny;
  14.         foreach (n, e; graph[currentVertex])
  15.         {
  16.             if (used[n] == 0)
  17.             {
  18.                 foreach (r; e)
  19.                 {
  20.                     path[n] = min(path[n], path[currentVertex] + r);
  21.                 }
  22.                 heap.insert(n);
  23.             }
  24.         }
  25.         used[currentVertex] = 1;
  26.     }
  27.  
  28.     return path;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement