Pagini recente » Cod sursa (job #1882571) | Cod sursa (job #922129) | Cod sursa (job #1761626) | Cod sursa (job #1771218) | Cod sursa (job #1501554)
#include<iostream>
#include<fstream>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
const int PCT=100002;
vector<int> gr[PCT];
int cost[PCT];
queue<int> q;
void bfs(int s)
{
q.push(s);
cost[s]=0;
int poz,i;
while(q.size())
{
poz=q.front();
q.pop();
for(i=0;i<gr[poz].size();++i)
{
if(cost[gr[poz][i]]==-1)
{
cost[gr[poz][i]]=cost[poz]+1;
q.push(gr[poz][i]);
}
}
}
}
int main()
{
ifstream si;
si.open("bfs.in");
//FILE*si=fopen("bfs.in","r");
ofstream so;
so.open("bfs.out");
int n,m,s;
si>>n>>m>>s;
int i,a,c;
for(i=0;i<m;++i)
{
si>>a>>c;
gr[a].push_back(c);
}
memset(cost,-1,sizeof cost);
bfs(s);
for(i=1;i<=n;++i)
so<<cost[i]<<' ';
so<<'\n';
si.close();
so.close();
return 0;
}