Cod sursa(job #1199293)

Utilizator breahnadavidBreahna David breahnadavid Data 18 iunie 2014 19:22:52
Problema BFS - Parcurgere in latime Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include<iostream>
#include<fstream>

using namespace std;

ifstream f("bfs.in");
ofstream g("bfs.out");

struct nod
        {
        int a;
        nod *next;
        }*t[100000],*r,*End;

int s,n,m,i,j,b[16002];

void parcurg()
        {

        nod *q,*p;
        b[r->a]=0;
        while(r!=NULL)
                {
                q=t[r->a];
                while(q!=NULL)
                        {
                        if(b[q->a]==0&&q->a!=s)
                        {
                        p=new nod;
                        p->a=q->a;
                        b[q->a]=b[r->a]+1;
                        p->next=NULL;
                        End->next=p;
                        End=p;
                        }
                        q=q->next;
                        }
                nod *w;
                w=r;
                r=r->next;
                delete(w);
                }
        }


int main()
{
f>>n>>m>>s;

while(f>>i>>j)
        {
        nod *q;
        q=new nod;
        q->a=j;
        q->next=t[i];
        t[i]=q;
        }

r=new nod;
r->a=s;
r->next=NULL;
End=r;

parcurg();

for(i=1;i<=n;i++)
if(i==s)g<<0<<' ';
else
if(b[i]==0)g<<-1<<' ';
else g<<b[i]<<' ';

g.close();
return 0;
}