ダイクストラ法
This can be done via Dijkstra's algorithm, dynamic programming, greedy algorithm or graph cuts among others.