Pagini recente » Cod sursa (job #2382156) | Cod sursa (job #1483048) | Istoria paginii runda/cnrv_x_1 | Cod sursa (job #1424269) | Cod sursa (job #2210412)
#include <fstream>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
bool a[1002][1002];
int n, m, s, d[1003];
int q[100003], pr, ul;
void BFS(int s)
{
int i, x;
pr = 0;
ul = -1;
q[++ul] = s;
for(i = 1; i <= n; i++)
d[i] = -1;
d[s] = 0;
while(pr <= ul)
{
x = q[pr];
pr++;
for(i = 1; i <= n; i++)
if(a[x][i] == true)
if(d[i] > d[x] + 1 || d[i] == -1)
{
d[i] = d[x] + 1;
q[++ul] = i;
}
}
}
int main()
{
int i, x, y;
fin >> n >> m >> s;
for(i = 1; i <= m; i++)
{
fin >> x >> y;
a[x][y] = true;
}
BFS(s);
for(i = 1; i <= n; i++)
fout << d[i] << " ";
return 0;
}