Implemented Dijkstra's algorithm