Cod sursa(job #2323076)
| Utilizator | Data | 18 ianuarie 2019 19:43:01 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.05 kb |
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("bfs.in");
ofstream fout("bfs.out");
unsigned n,m,s,x,y;
bool v[100005];
vector<int> gf[100005];
unsigned nr;
int main()
{
fin>>n>>m>>s;
for(unsigned i=1;i<=m;i++)
{
fin>>x>>y;
gf[x].push_back(y);
}
for(int i=1;i<=n;i++)
{
queue <int> q;
q.push(s);
nr=0;
if(i<n)fill(v+i+1,v+n+1,0);
else fill(v+i,v+n+1,0);
while(!q.empty())
{
int nod=q.front();
q.pop();
if(nod==i)
{
fout<<nr<<' ';
break;
}
else nr++;
for(auto j:gf[nod])
{
if(!v[j])
{
v[j]=1;
q.push(j);
}
}
if(q.empty())fout<<"-1"<<' ';
}
}
return 0;
}
