implemented Dijkstra's algorithm