Cod sursa(job #349641)

Utilizator georgelRector George georgel Data 20 septembrie 2009 19:09:26
Problema BFS - Parcurgere in latime Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<fstream>
#define Max 10000

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][y] = 1;
     }
}
void bf(){
    int st,dr,i;
    st = 1;
    dr = 1;
     while(st <= dr){
         for(i = 1; i <= n; i++)
         if(!pus[i] && a[b[st]][i] == 1)
         {
                    dr++;
                    b[dr] = i;
                    c[i] = c[b[st]]+1;
                    pus[i] = 1;
         }
st++;
}
}
int main(){
read();
pus[s] = 1;
c[s] = 0;
b[1] = s;
bf();
int i;
for(i = 1;i <= n; i++)
if(pus[i] == 0)
c[i] = -1;
for(i = 1;i <= n; i++)
fout<<c[i]<<" ";
fin.close();
fout.close();
return 0;
}