Pagini recente » Cod sursa (job #2890267) | Cod sursa (job #2886401) | Cod sursa (job #1671411) | Cod sursa (job #459394) | Cod sursa (job #1974137)
#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
struct graph{vector <int> v;};
queue <int> q;
graph G[100001];
int cost[100001];
inline void BFS(const int &x)
{
int i,nod;
q.push(x);
cost[x]=0;
while(!q.empty())
{
nod=q.front();
q.pop();
for(i=0;i<G[nod].v.size();i++)
if(cost[nod]+1<cost[G[nod].v[i]])
{
cost[G[nod].v[i]]=cost[nod]+1;
q.push(G[nod].v[i]);
}
}
}
int main()
{int n,m,start,k,i,j;
f>>n>>m>>start;
for(k=0;k<m;k++)
{
f>>i>>j;
G[i].v.push_back(j);
}
fill(cost+1,cost+n+1,INF);
BFS(start);
replace(cost+1,cost+n+1,INF,-1);
for(i=1;i<=n;i++)
g<<cost[i]<<' ';
return 0;
}