OSDN Git Service
(root)
/
oi-wiki
/
main.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
8475659
)
feat: 输出方案
author
Ir1dXD
<sirius.caffrey@gmail.com>
Sat, 1 Dec 2018 02:45:22 +0000
(10:45 +0800)
committer
GitHub
<noreply@github.com>
Sat, 1 Dec 2018 02:45:22 +0000
(10:45 +0800)
docs/graph/shortest-path.md
patch
|
blob
|
history
diff --git
a/docs/graph/shortest-path.md
b/docs/graph/shortest-path.md
index
e5f985b
..
f9f5823
100644
(file)
--- a/
docs/graph/shortest-path.md
+++ b/
docs/graph/shortest-path.md
@@
-263,6
+263,12
@@
for (i = 1; i <= n; i++) {
| 没有负环的图 | 任意图 | 非负权图 |
| $O(N^3)$ | $O(NM)$ | $O((N+M)\log M)$ |
+## 输出方案
+
+开一个 `pre` 数组,在更新距离的时候记录下来后面的点是如何转移过去的,算法结束前再递归地输出路径即可。
+
+比如 Floyd 就要记录 `pre[i][j] = k;`,Bellman-Ford 和 Dijkstra 一般记录 `pre[v] = u`。
+
## 拓展:分层图最短路
分层图最短路,一般模型为有 $k$ 次零代价通过一条路径,求总的最小花费。对于这种题目,我们可以采用 DP 相关的思想,设 $\text{dis}_{i, j}$ 表示当前从起点 $i$ 号结点,使用了 $j$ 次免费通行权限后的最短路径。显然,$\text{dis}$ 数组可以这么转移: