Cod sursa(job #1007509)
| Utilizator | Data | 8 octombrie 2013 23:22:11 | |
|---|---|---|---|
| Problema | BFS - Parcurgere in latime | Scor | 50 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.76 kb |
#include<fstream>
using namespace std;
ifstream f("bfs.in");
ofstream g("bfs.out");
bool a[1005][1005],viz[100005];
int v[100005],cost[100005];
int main()
{
int n,m,s; int p=1,u=1,nod; int x,y; int i,j;
f>>n>>m>>s; for(i=1;i<=n;i++)cost[i]=-1;
while(m!=0){f>>x>>y;a[x][y]=1;m--;}
v[p]=s;viz[s]=1;cost[s]=0;
while(p<=u)
{
nod=v[p];
for(i=1;i<=n;i++)
//if(i!=nod)
if(a[nod][i]==1&&viz[i]==0)
{
u++;
v[u]=i;
viz[i]=1;
cost[i]=cost[nod]+1;
}
p++;
}
for(i=1;i<=n;i++)g<<cost[i]<<" ";
return 0;
}
