Merged Dijkstra code.