Problem
Figure 1. The graph from the dataset
The task is to use Bellman-Ford algorithm to compute single-source shortest distances in a directed graph with possibly negative edge weights (but without negative cycles).
Given: A simple directed graph with integer edge weights from to and vertices in the edge list format.
Return: An array where is the length of a shortest path from the vertex to the vertex (). If is not reachable from set to x
.
See Figure 1 for visual example from the sample dataset.
任务是使用Bellman-Ford算法来计算有向图中可能具有负边权重(但没有负循环)的有向图中的单源最短距离。
给定:一个简单的有向图,其边权重为 至 和 边列表格式的顶点。
返回:数组 哪里 是从顶点开始的最短路径的长度 到顶点 ()。如果 从以下位置无法到达 组 到x
。
有关示例数据集中的可视示例,请参见图1。
Sample Dataset
9 13 1 2 10 3 2 1 3 4 1 4 5 3 5 6 -1 7 6 -1 8 7 1 1 8 8 7 2 -4 2 6 2 6 3 -2 9 5 -10 9 4 7
Sample Output
0 5 5 6 9 7 9 8 x