Mai intai trebuie sa te autentifici.
Cod sursa(job #2492470)
| Utilizator | Data | 14 noiembrie 2019 20:00:58 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.77 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
int mtA[100001][100001], dist[100001], val[100001];
int main()
{
int N, M,S;
f >> N >> M >> S;
int v[101][3];
while (M--) {
int x, y;
f >> x >> y;
if(x != y)
mtA[x][y] = 1;
}
int t = 1;
for (int i = 1; i <= N; i++)
dist[i] = -1;
dist[S] = 0;
val[1] = S;
for(int x = 1; x <= t; x++) {
for(int j = 1; j <= N; j++) {
if(mtA[val[x]][j] == 1 && dist[j] == -1) {
dist[j] = dist[val[x]] + 1;
val[++t] =j;
}
}
}
for(int i = 1; i <= N; i++)
g << dist[i] << " ";
return 0;
}
