Slightly smaller implementation of Dijkstra's algorithm.