Cod sursa(job #1033397)

Utilizator ValeraShulghin Valera Valera Data 16 noiembrie 2013 21:11:57
Problema Sate Scor 45
Compilator fpc Status done
Runda cei_mici3 Marime 1.1 kb
type vector=record
     i,d:longint;
     end;
type sate=record
     i,j,d:longint;
     end;
var a:array[0..110000] of sate;
    b:array[0..110000] of vector;
    q:array[0..110000] of longint;
    i,n,m,x,y,p,u,k:longint;
    f,g:text;
begin
assign(f,'sate.in');reset(f);
assign(g,'sate.out');rewrite(g);
readln(f,n,m,x,y);
for i:=1 to m do
  readln(f,a[i].i,a[i].j,a[i].d);
b[0].i:=x;b[0].d:=0;
p:=0;u:=0;
while p<=u do
  begin
    for i:=1 to m do
      begin
        if (b[p].i=a[i].i) and (q[a[i].j]=0) then
          begin
            u:=u+1;
            b[u].i:=a[i].j;
            if b[u].i>a[i].i then b[u].d:=b[p].d+a[i].d
            else b[u].d:=b[p].d-a[i].d;
            if b[u].i=y then k:=b[u].d;
          end;
        if (b[p].i=a[i].j) and (q[a[i].i]=0) then
          begin
            u:=u+1;
            b[u].i:=a[i].i;
            if b[u].i>a[i].j then b[u].d:=b[p].d+a[i].d
            else b[u].d:=b[p].d-a[i].d;
            if b[u].i=y then k:=b[u].d;
          end;
      end;


    q[b[p].i]:=1;
    p:=p+1;
  end;
writeln(g,k);
close(f);close(g);
end.