Cod sursa(job #2243961)

Utilizator pinteastefanPintea Teodor Stefan pinteastefan Data 21 septembrie 2018 19:28:54
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

vector <long > graff[100001];
bool visited [100010];
int cost [100001];
queue <int > qq;


int main() {
    ifstream f("bfs.in");
    ofstream g("bfs.out");
    int n, m, s;
    f >> n >> m >> s;
    for (int i = 1; i <= m; i ++) {
        int x, y;
        f >> x >> y;

        graff[x].push_back(y);
    }

    visited[s] = true;
    qq.push(s);
    for ( int i = 1 ; i <= n; i++){
        cost[i] = (1 << 29);
    }
    cost[s] = 0;

    while (!qq.empty()){
        int frontValue = qq.front();
        // g << frontValue << " ";
        qq.pop();
        for ( auto vecin : graff[frontValue]){
           /* if (visited[vecin] == false){
                visited[vecin] = true;
            }*/
            if ( cost [vecin] > cost[frontValue] + 1){
                cost[vecin] = cost[frontValue] + 1;
                qq.push(vecin);
            }
        }
    }
    for ( auto &x : cost){
        if( x == (1 << 29)){
            x = -1;
        }
    }

    for (int i =1; i<= n; i++){
        g << cost[i] << " ";
    }
    return 0;
}