在一张有非负边权的有向图中,我可以轻松使用Dijkstra算法找到从u到v的最短路径。但是,是否有任何简单的修改Dijkstra算法的方法,以便我可以通过给定的顶点w找到从u到v的最短路径。或者有其他算法建议吗?
然后,u->w->v就是最短路径。
你可以运行两次Dijkstra算法来完成,但也可以应用Floyd-Warshall算法。
set v_visisted = false
Start from w and find shortest path to u
if v was visited during shortest path search to u, set v_visted = true
If v is part of shortest path from w->u then
exit with result ( # the path would be u->v->w->v )
else
if v_visited= true then we already know values for w->v. We have a solution.
else save path from w->v and switch u to source and find shortest path to v.
看起来需要先找到 u 到 w,然后再找到 w 到 v,将两个结果连接起来。这样行得通吗?