added a Dijkstra-like solution