Pagini recente » Cod sursa (job #1021719) | Cod sursa (job #2065730) | Cod sursa (job #2252166) | Cod sursa (job #661176) | Cod sursa (job #1676779)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <limits.h>
#define MAX 100003
using namespace std;
ifstream in("bfs.in");
ofstream out("bfs.out");
vector<int> nod[MAX];
int mi[MAX];
queue<pair<int, int>> qu;
int main() {
int n,m,s,x,y;
in >> n >> m >> s;
for(int i = 1; i <= m; i++) {
in >> x >> y;
nod[x].push_back(y);
}
for(int i = 1; i <= n; i++)
mi[i] = INT_MAX;
qu.push(make_pair(s, 0));
pair<int, int> temp;
vector<int>::iterator it;
while(!qu.empty()) {
temp = qu.front();
qu.pop();
mi[temp.first] = temp.second;
for(it = nod[temp.first].begin(); it != nod[temp.first].end(); it++) {
if(temp.second+1 < mi[*it]) {
qu.push(make_pair(*it, temp.second+1));
}
}
}
for(int i = 1; i <= n; i++) {
if(mi[i] == INT_MAX)
mi[i] = -1;
out << mi[i] << " ";
}
return 0;
}