我有一张代表城市的图表。一些顶点是医院。图表已连接。
我正在寻找一种算法,该算法将为我提供从任何给定节点到最近医院的路径(甚至距离)。
可以想到计算所有最短路径,但我认为它们可能是更聪明的方式。
由于
答案 0 :(得分:1)
我想你需要看warshall's algorithm for shortest path
这是算法
let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)
for each vertex v
dist[v][v] ← 0
for each edge (u,v)
dist[u][v] ← w(u,v) // the weight of the edge (u,v)
for k from 1 to |V|
for i from 1 to |V|
for j from 1 to |V|
if dist[i][k] + dist[k][j] < dist[i][j] then
dist[i][j] ← dist[i][k] + dist[k][j]
上述算法在左下方的图表上执行
[1]:http://i.stack.imgur.com/3J7Sb.png
参考:wikiPedia (链接在答案开始时给出)
有关详细信息请访问链接