Pagini recente » Cod sursa (job #1435916) | Cod sursa (job #1179083) | Cod sursa (job #15609) | Cod sursa (job #2934736) | Cod sursa (job #1795034)
#include <stdio.h>
#include <queue>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <vector>
#define INF 0x3f3f3f3f
#define NM 100005
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
vector < int > a[NM];
queue < int > coada;
int l[NM], cost[NM];
int n, m, st;
void BFS(int nod)
{
int nod2, minn;
for(int i = 1; i <= NM - 1; ++i)
cost[i] = -1;
cost[nod] = 0;
coada.push(nod);
while(!coada.empty())
{
nod2 = coada.front();
coada.pop();
for(int i = 0; i < l[nod2]; ++i)
{
if(cost[ a[nod2][i] ] == -1)
cost[a[nod2][i]] = cost[nod2] + 1, coada.push(a[nod2][i]);
}
}
}
int main()
{
int x, y;
f >> n >> m >> st;
for(int i = 1; i <= m; ++i)
{
f >> x >> y;
a[x].push_back(y);
l[x]++;//liste de adiacenta..l[i] - dimensiunea vectorului a[i]
}
BFS(st);
for(int i = 1; i <= n; ++i)
g << cost[i] << ' ';
}