算法题,进链接看图片和文本,不限语言c/c++/python或伪码,会的留下代码或解决方式最好带注释
问题描述:
链接: https://pan.baidu.com/s/1Tls9JFQqoQvXKdbAY7sa3w
提取码: in8e
答
因为边的权重可能为负值,所以Dijkstra算法不适用。可以使用Bellman–Ford算法计算最短路径,不需要任何改动。题目中额外要求,如果有多条最短路径,选择边数最少的一条,Bellman–Ford算法刚好也满足这个要求,因为该算法总是按照边数从少到多的顺序更新最短路径。代码及证明 https://en.wikipedia.org/wiki/Bellman–Ford_algorithm
答
Consider a graph on three nodes s, u, t where s is the source node. Assume that the graph has edges
(s, u) with weight 1
(u, t) with weight 1
(s, t) with weight 2
Assume further that B-F takes the edges in the above order, then it would find s, u, t as a shortest path, but there is another just s, t which has weight 2 as well.