Cod sursa(job #1167428)

Utilizator andi23roscaRosca Andrei andi23rosca Data 4 aprilie 2014 23:16:41
Problema BFS - Parcurgere in latime Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
typedef struct muchii
{
    long long x,y;
};
muchii muchie[100010];
ifstream f("bfs.in");
ofstream g("bfs.out");
std::queue<long long> stiva;
long long n,m,s,i,j,v[100010],c[100010],distanta=0;
void citire()
{
    f>>n>>m>>s;
    for(i=1;i<=m;i++)
    {
        f>>muchie[i].x>>muchie[i].y;
    }
}

void bfs()
{
    long long nod=stiva.front();
    v[stiva.front()]=1;
    distanta=c[nod]+1;
    stiva.pop();
    for(i=1;i<=m;i++)
    {
        if(muchie[i].x == nod && v[muchie[i].y]!=1)
        {
            stiva.push(muchie[i].y);
            c[muchie[i].y]=distanta;
        }
    }
    if(!stiva.empty()) bfs();
}

int main()
{
    citire();
    stiva.push(s);
    bfs();
    for(i=1;i<=n;i++)
    {
        if(c[i]==0 && i!=s) g<<-1<<" ";
        else g<<c[i]<<" ";
    }
    return 0;
}