straitforward implementation of Dijkstra's algorithm