Implemented Dijkstra's algorithm (faster)