Cod sursa(job #2424555)

Utilizator mihaidanielmihai daniel mihaidaniel Data 23 mai 2019 11:25:36
Problema BFS - Parcurgere in latime Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

int b[100001];
vector <int> mat[100001];
priority_queue <pair <int, int> > q;
pair <int, int> p;
vector <int> :: iterator iv, fv;

int main()
{
    //ifstream in ("date.in");/*
    ifstream in ("bfs.in");
    ofstream out ("bfs.out");//*/
    int n, m, s, i, x, y;
    in >> n >> m >> s;
    q.push({-(b[s] = 1), s});
    for (i = 0; i < m; ++i) {
        in >> x >> y;
        mat[x].push_back (y);
    }

    while (!q.empty()) {
        p = q.top();
        q.pop();
        fv = mat[p.second].end();
        for (iv = mat[p.second].begin(); iv != fv; ++iv) {
            if (b[*iv] == 0) {
                q.push ({-(b[*iv] = b[p.second] + 1), *iv});
            }
        }
    }

    for (i = 1; i <= n; ++i) {
        out << b[i] - 1 << " ";
    }
    return 0;
}