问题及代码:
/*广度优先搜索算法-最少转机*/
#include<stdio.h>
struct note
{
int x; //城市编号
int s; //转机次数
};
int main()
{
int i,j,n,m,a,b,cur,e[51][51]={0},book[51]={0},head,tail;
int start,end,flag=0;
struct note que[2501];
scanf("%d %d",&n,&m,&start,&end);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j) e[i][j]=0;
else e[i][j]=9999999;
for(i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
e[a][b]=1;
e[b][a]=1;
}
head=1;
tail=1;
que[tail].x=start;
que[tail].s=0;
tail++;
book[1]=start; //标记start号城市已在队列中
while(head<tail)
{
cur=que[head].x;
for(i=1;i<=n;i++)
{
if(e[cur][i]==1 && book[i]==0)
{
que[tail].x=i;
que[tail].s=que[head].s+1; //转机次数+1
tail++;
book[i]==1;
}
if(que[tail].x==end)
flag=1;
break; //退出内循环
}
if(flag==1)
break; //退出外循环
head++;
}
printf("%d",que[tail-1].s); //输出最少的转机次数
getchar();getchar();
return 0;
}
结果出错!为何?