Cod sursa(job #349540)

Utilizator georgelRector George georgel Data 20 septembrie 2009 00:02:15
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<fstream>
#define Max 1000

using namespace std;

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

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