dp最短路径算法伪代码
时间: 2023-09-14 17:16:00 浏览: 224
下面是Dijkstra算法的伪代码:
```
function Dijkstra(Graph, source):
create vertex set Q
for each vertex v in Graph:
dist[v] ← INFINITY
prev[v] ← UNDEFINED
add v to Q
dist[source] ← 0
while Q is not empty:
u ← vertex in Q with min dist[u]
remove u from Q
for each neighbor v of u: // only v that is still in Q
alt ← dist[u] + length(u, v)
if alt < dist[v]:
dist[v] ← alt
prev[v] ← u
return dist[], prev[]
```
其中`Graph`是一个加权有向图,`source`是起始节点。`dist`是源节点到各个节点的最短距离,`prev`是保存路径的数组。
阅读全文