Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- auto dijkstra(int[][int][int] graph, int start)
- {
- auto used = new int[graph.length];
- auto path = new int[graph.length];
- path[] = int.max; path[start] = 0;
- auto heapRange = new int[graph.length];
- auto heap = heapify!((a, b) => path[a] > path[b])(heapRange, 0);
- heap.insert(start);
- while (!heap.empty)
- {
- auto currentVertex = heap.removeAny;
- foreach (n, e; graph[currentVertex])
- {
- if (used[n] == 0)
- {
- foreach (r; e)
- {
- path[n] = min(path[n], path[currentVertex] + r);
- }
- heap.insert(n);
- }
- }
- used[currentVertex] = 1;
- }
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement