Implemented Dijkstra's algorithm.