Cod sursa(job #135045)

Utilizator CezarMocanCezar Mocan CezarMocan Data 12 februarie 2008 20:39:32
Problema Sate Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.52 kb
var id,cs,ct,viz,x,s,p,q:array[0..200010] 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(p[i],q[i],viz[i]);
        inc(id[p[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[p[i]]);
        inc(ct[q[i]]);
        x[id[p[i]-1]+ct[p[i]]]:=q[i];
        x[id[q[i]-1]+ct[q[i]]]:=p[i];
        cs[id[p[i]-1]+ct[p[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.