Cod sursa(job #459953)

Utilizator BitOneSAlexandru BitOne Data 31 mai 2010 18:34:00
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define Nmax 100111

/*
 *
 */
using namespace std;
int d[Nmax];
queue< int > Q;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
int main( void )
{
    int N, M, s, x, y;
    ifstream in( "bfs.in" );
    for( in>>N>>M>>s; M; --M )
    {
        in>>x>>y;
        G[x].push_back(y);
    }
    for( Q.push(s); !Q.empty(); )
    {
        x=Q.front(); Q.pop();
        for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
            if( s != *it && !d[*it] )
            {
                d[*it]=d[x]+1;
                Q.push(*it);
            }
    }
    replace( d+1, d+N+1, 0, -1 );
    d[s]=0;
    ofstream out( "bfs.out" );
    copy( d+1, d+N+1, ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return EXIT_SUCCESS;
}