Cod sursa(job #1168435)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 8 aprilie 2014 16:36:29
Problema Sate Scor 15
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.28 kb
program sate;
 const nmax=30000;
       inf=1 shl 30;
 type lista=^celula;
        celula=record
            info:longint;
            cost:longint;
            pred:lista;
            end;
 var a:array[0..nmax] of lista;
     d,v,c:array[0..nmax] of longint;
     n,m,i,x,y,z,p,u,i1,j1:longint;
     r:lista;
     buf:array[1..1 shl 16] of byte;
 begin
  assign(input,'sate.in');
  assign(output,'sate.out');
  reset(input);
  rewrite(output);
  settextbuf(input,buf);
  readln(n,m,i1,j1);
  for i:=1 to n do
   begin
    readln(x,y,z);
    new(r);
    r^.info:=y;
    r^.cost:=z;
    r^.pred:=a[x];
    a[x]:=r;
    new(r);
    r^.info:=x;
    r^.cost:=z;
    r^.pred:=a[y];
    a[y]:=r;
   end;

  p:=1; u:=1;
  c[1]:=i1;
  for i:=1 to n do begin d[i]:=inf; v[i]:=0; end;
  v[c[p]]:=1;
  d[i1]:=0;
  while p<=u do
   begin
      r:=a[c[p]];
      while r<>nil do
       begin
        if v[r^.info]=0 then
         begin
           v[r^.info]:=1;
           inc(u);
           c[u]:=r^.info;
           if c[u]<c[p] then d[c[u]]:=d[c[p]]-r^.cost
           else
           d[c[u]]:=d[c[p]]+r^.cost;
         end;
        r:=r^.pred
        end;
      p:=p+1;
   end;
   if d[j1]=inf then write(-1)else
   writeln(d[j1]);
   close(output);
  end.