Cod sursa(job #135050)

Utilizator CezarMocanCezar Mocan CezarMocan Data 12 februarie 2008 20:43:26
Problema Sate Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.55 kb
var id,cs,x,s,q:array[0..200010] of longint;
    ct,viz:array[1..30001] of longint;
    a,b,i,j,n,m,u,t,pe:longint;

begin
assign(input,'sate.in');reset(input);
assign(output,'sate.out');rewrite(output);
readln(n,m,a,b);
for i:=1 to m do
        begin
        readln(s[i],q[i],viz[i]);
        inc(id[s[i]]);
        inc(id[q[i]]);
        end;
for i:=1 to n do
        id[i]:=id[i]+id[i-1];
for i:=1 to m do
        begin
        inc(ct[s[i]]);
        inc(ct[q[i]]);
        x[id[s[i]-1]+ct[s[i]]]:=q[i];
        x[id[q[i]-1]+ct[q[i]]]:=s[i];
        cs[id[s[i]-1]+ct[s[i]]]:=viz[i];
        cs[id[q[i]-1]+ct[q[i]]]:=viz[i];
        end;
fillchar(ct,sizeof(ct),0);
fillchar(viz,sizeof(viz),0);
s[1]:=a;
pe:=1;u:=1;
while pe<=u do
        begin
        for i:=id[s[pe]-1]+1 to id[s[pe]] do
                begin
                if viz[x[i]]=0 then
                        begin
                        inc(u);
                        s[u]:=x[i];
                        viz[x[i]]:=1;
                        if x[i]<s[pe] then
                                ct[x[i]]:=ct[s[pe]]-cs[i]
                        else
                                ct[x[i]]:=ct[s[pe]]+cs[i];
                        if x[i]=b then
                                begin
                                writeln(ct[x[i]]);
                                close(output);
                                halt;
                                end;
                        end;
                end;
        inc(pe);
        end;
close(input);close(output);
end.