您的位置:首页 > 其它

文章标题

2016-06-21 13:23 232 查看
#include<stdio.h>
#include<string.h>
struct node
{
int x,y;
int ans;
}q[1001];
int map[1001][1001];
int book[1001];
int m,n;
void bfs( int x)
{
int head = 0,tail = 0,i;
struct node t,f;
t.x=x;
t.ans = 0;
q[tail++]=t;
book[t.x] = 1;
while(head < tail)
{
t = q[head++];
if(t.x == 1)
{
printf("%d\n",t.ans);
return;
}

for( i =1; i <= n; i++)
{
f.x = i;
if(!book[f.x]&&map[t.x][f.x]==1)
{
f.ans=t.ans+1;
q[tail++] = f;
book[f.x]=1;
}

}
}
printf("NO\n");
return;
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
memset(book,0,sizeof(book));
while(m--)
{
scanf("%d%d",&a,&b);
map[a][b]=1;
}
bfs(n);
}
}


啦啦啦啦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: