Cod sursa(job #590846)

Utilizator vendettaSalajan Razvan vendetta Data 20 mai 2011 15:17:37
Problema BFS - Parcurgere in latime Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.47 kb
const f ='bfs.in'; g = 'bfs.out';
var
    nvec,cost,S : array[0..100001] of longint;
    a : array of array of longint;
    //a : array[0..10001,0..10001] of longint;
    ii, jj, n, m : longint;
    st, x, y : longint;

procedure bfs( nod : longint );
    var
        l2, L, i, j, ss : longint;
    begin
        for i := 0 to n do cost[i] := -1;
        //fillchar(cost, sizeof(cost),1);
        L := 1;
        cost[nod] := 0;
        S[L] := nod;
        ss := 0;
        l2 := 1 ;
        //for i := l2 to L do begin
        while ss<=l do begin
            for j := 1 to nvec[S[ss]] do
                if cost[a[S[ss]][j]] = -1 then begin
                    inc( L );
                    S[L] := a[S[ss]][j];
                    //inc( L );
                    cost[S[L]] := cost[S[ss]] + 1;
                end;
            inc( ss );
        end;
            //inc( ss );
        //end;
        //writeln( l );
    end;

begin
    assign( input,f ); reset( input );
    assign( output,g ); rewrite( output );
    readln( n, m, st );
    setlength(a,n+1,n);
    for ii := 1 to m do begin
        readln( x,y );
        nvec[x] := nvec[x] + 1;
        //nvec[y] := nvec[y] + 1;
        a[x][nvec[x]] := y;
        //a[y,nvec[y]] := x;
    end;
    {
    for ii := 1 to n do begin
        for jj := 1 to nvec[ii] do write(a[ii,jj],' ');
        writeln;
    end;
    writeln;
    }
    bfs(st);
    for ii := 1 to n do write(cost[ii],' ');
end.