Pagini recente » Cod sursa (job #344226) | Cod sursa (job #1925922) | Cod sursa (job #3235597) | Cod sursa (job #2244494) | Cod sursa (job #717799)
Cod sursa(job #717799)
#include<iostream>
#include<fstream>
using namespace std;
# define max 100
struct nod{
int vec;
nod *next;
};
struct coada{
int cost;
int vf;
coada *next;
}*prim,*q;
struct vector{
int a;
int b;
}queue[max];
ifstream f("bfs.in");
ofstream g("bfs.out");
int co[max]={0},n,m,s,viz[max]={0};
int st=1,dr=0;
nod *v[max];
void push(int a, int cost){
dr++;
queue[dr].a=a;
queue[dr].b=++cost;
co[a]=cost;
}
int pop(){
st++;
return queue[st-1].b;
}
void citire(){
f>>n>>m>>s;
for(int i=1;i<=n;i++)
v[i]=NULL;
for(int i=1;i<=m;i++){
int x,y;
f>>x>>y;
if(v[x]==NULL){
v[x]=new nod;
v[x]->vec=y;
v[x]->next=NULL;
}
else{
nod *p=new nod;
p->vec=y;
p->next=v[x];
v[x]=p;
}
}
}
int main(){
q=NULL;
citire();
push(s,-1);
viz[s]=1;
while(st<=dr){
nod *r=v[queue[st].a];
int c=pop();
while(r!=NULL){
if(viz[r->vec]==0){
push(r->vec,c);
viz[r->vec]=1;
}
r=r->next;
}
}
for(int i=1;i<=n;i++)
if(i==s)
g<<0<<" ";
else
if(co[i]==0)
g<<-1<<" ";
else
g<<co[i]<<" ";
return 0;
}