判断图中是否有负权回路 Bellman-ford 算法 x[I] y[I] t[I]分别表示第I条边的起点 终点和权。
回路,终点,算法
已帮助 人
判断图中是否有负权回路 Bellman-ford 算法
x[I],y[I],t[I]分别表示第I条边的起点,终点和权。共n个结点和m条边。
procedure bellman-ford
-
正确答案:
begin
for I:=0 to n-1 do d[I]:=+infinitive;
d[0]:=0;
for I:=1 to n-1 do
for j:=1 to m do {枚举每一条边}
if d[x[j]]+t[j]<d[y[j]] then d[y[j]]:=d[x[j]]+t[j];
for I:=1 to m do
if d[x[j]]+t[j]<d[y[j]] then return false else return true;
end;
