Cod sursa(job #1093447)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 27 ianuarie 2014 23:46:03
Problema BFS - Parcurgere in latime Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 0.75 kb
program bfs;
var a:array[1..10000,1..10000] of 0..1;
    viz,c,cost:array[-5.. 1000000] of longint;
    n,m,i,x,y,s,v,st,dr:longint;
begin
assign(input,'bfs.in'); reset(input);
assign(output,'bfs.out'); rewrite(output);
readln(n,m,s);

for i:=1 to m do begin
                readln(x,y);
                a[x,y]:=1;
                end;
for i:=1 to n do cost[i]:=-1;
cost[s]:=0;
st:=1; dr:=1;
c[1]:=s;
viz[s]:=1;
while (st<=dr) do begin
       for i:=1 to n do
         if (a[c[st],i]=1) and (viz[i]=0) then
           begin
           inc(dr);
           c[dr]:=i;
           viz[i]:=1;
           cost[i]:=cost[c[st]]+1;
           end;
       inc(st);
                  end;

for i:=1 to n do write(cost[i],' ');
close(output);
end.