Cod sursa(job #1377033)

Utilizator gerd13David Gergely gerd13 Data 5 martie 2015 19:48:37
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std ;

const int NMAX =  100005 ;
const int INF = 0x3f3f3f3f ;

ifstream fin("bfs.in") ;
ofstream fout("bfs.out") ;

vector <int> V[NMAX] ;
queue <int> Q ;
int S, N, M,  cost[NMAX];
int cnt = 0 ;



void BFS(int nod)
{
    Q.push(nod) ;
    cost[nod] = 0 ;
    while(!Q.empty())
    {
        nod = Q.front() ;
        Q.pop() ;
        for(int i = 0 ; i < V[nod].size() ; ++ i)
            if(cost[V[nod][i]] == -1)
        {
            cost[V[nod][i]] = cost[nod] + 1 ;
                   Q.push(V[nod][i]) ;
        }
}

}

int main()
{
    fin >> N >> M >> S ;

    for(int i = 1 ; i <= M ; ++ i)
    {

        int XX, YY ;
        fin >> XX >> YY ;

        V[XX].push_back(YY) ;
    }

    memset(cost,  -1, sizeof cost) ;

    BFS(S) ;


    for(int i = 1 ; i <= N ; ++ i)
        fout << cost[i] << ' ' ;

    fin.close() ;
    fout.close() ;
    return  0 ;
}