iliya785

Bellman-Ford

May 21st, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.52 KB | None | 0 0
  1. type
  2.     edge = record
  3.         a,b,cost:longint;
  4. end;
  5.  
  6. const
  7.    count = 1000;
  8.    inf = 1000000;
  9.  
  10. var
  11.     e:array[1..count] of edge;
  12.     d:array[1..count] of longint;
  13.     i,j,n,m,v1,v2:longint;
  14.  
  15. Begin
  16.   read(n,m,v1,v2);
  17.   for i:=1 to m do
  18.     read(e[i].a,e[i].b,e[i].cost);
  19.   for i:=1 to n do
  20.     d[i]:=inf;
  21.   d[v1]:=0;
  22.   for i:=1 to n do
  23.      for j:=1 to m do
  24.        if (d[e[j].b] > d[e[j].a] + e[j].cost) and (d[e[j].a] < inf) then
  25.            d[e[j].b] := d[e[j].a] + e[j].cost;
  26.   write(d[v2]);
  27. end.
Advertisement
Add Comment
Please, Sign In to add comment