Menu
CoddyTech

最终挑战 #2

Coddy Bellman-Ford 算法:图算法 课程的第 9 课,共 9 课。

challenge icon

挑战

中等

再来一个挑战:查询带有负权边的点对之间的距离。

编写一个名为 shortestDistance 的函数,它接受 n、扁平的 edges 数组(由三元组组成、有向边,权重可能为负)、一个 source 和一个 target,并返回从 source 到 target 的最短距离;如果无法到达 target,则返回 -1。假设不存在负权环。

亲手试试

#include <stdlib.h>

int shortestDistance(int n, int* edges, int edges_size, int source, int target) {
    // 在这里编写代码
    return -1;
}

Bellman-Ford 算法:图算法中的所有课

自己练习: C 在线编译器