Pagini recente » Profil itm_lucian_jan_filimon | Profil itm_soldan_paul | Profil fabogdan | Cod sursa (job #1179998) | Cod sursa (job #3224169)
#include <fstream>
#include <climits>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("mindist.in");
ofstream fout("mindist.out");
const int Nmax = 1005;
int n, m, s;
vector<int> dist(Nmax, -1), A[Nmax];
queue<int> Q;
void Lee() {
while(!Q.empty()) {
int cell = Q.front();
Q.pop();
for(int vec : A[cell]) {
if(dist[vec] == -1) {
dist[vec] = dist[cell] + 1;
Q.push(vec);
}
}
}
}
int main() {
fin >> n >> m >> s;
while(m--) {
int x, y;
fin >> x >> y;
A[x].push_back(y);
}
Q.push(s);
dist[s]=0;
Lee();
for(int i = 1; i <= n; i++)
fout << dist[i] << " ";
return 0;
}