Cod sursa(job #349950)

Utilizator georgelRector George georgel Data 21 septembrie 2009 21:47:07
Problema BFS - Parcurgere in latime Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include<fstream>
#define Max 10001

using namespace std;

ifstream fin("bfs.in");
ofstream fout("bfs.out");

int n,m,s,a[Max][Max],b[Max],c[Max],pus[Max];
void read(){
     int x,y;
     fin>>n>>m>>s;
     for(int i = 1; i <= m; i++)
     {
             fin>>x>>y;
             a[x][0]++;
             a[x][a[x][0]] = y;
     }
fin.close();
}
int gasit(int lin,int nod){
    for(int i = 1; i <= a[lin][0]; i++)
    if(a[lin][i] == nod)
    return 1;
return 0;
}
void bf(){
     int i,st,dr;
     st = 1;
     dr = 1;
   while(st <= dr){
           for(i = 1; i <= a[b[st]][0]; i++)
                      if(!pus[a[b[st]][i]])
                      {
                                        dr++;
                                        b[dr] = a[b[st]][i];
                                        pus[a[b[st]][i]] = 1;
                                        c[a[b[st]][i]] = c[b[st]]+1;
                      }
           st++;
   }
}
int main(){
    read();
    pus[s] = 1;
    b[1] = s;
    bf();
for(int i = 1; i <= n; i++)
if(pus[i] == 0)
c[i] = -1;
for(int i = 1; i <= n; i++)
fout<<c[i]<<" ";
fout.close();
return 0;
}