The graph of data structure Experiment five: The shortest steps from the starting point to the target point (BFS) OJ

Source: Internet
Author: User
Tags printf time limit
The graph of data structure Experiment five: The shortest steps from the starting point to the target point (BFS)Time limit:1000ms Memory limit:65536kb problem Description

In the ancient Legend of Warcraft, there are two legions, one called the Scourge, and the other called Janissary. In their area, there are n passes, numbered 1. N, there is a channel connection between some passes. Among them, the Legion was in the 1th, and the Scourge was in the N. One day, the leader of the Scourge, the Lich King, decided to send troops to attack the Guards Regiment, which is so large that the scourge can even fill the river. But the Lich King did not want to pay an unnecessary price, and he wondered whether, without the construction of any passages, the troops could reach the Guards regiment through the pass and its associated passages, and if so, how many channels would be required at least. Since the value of n is larger (n<=1000), the Lich King has found you =_= good at programming, ask you to help him solve this problem, or you will eat into his magic. To save yourself, find a way.

Input

The input contains multiple groups, each set in the following format.

The first line contains two integer n,m (representing n passes, each of which has m channels).

The following m lines each row contains two integers, a, a, a, or a channel from a to the B pass (note: The channel is unidirectional).

Output

If the scourge can reach the No. 1th pass without building any channels, the output will be at least as many channels as otherwise output No.

Example Input

2 1
1 2
2 1
2 1
Example Output
NO
1
Hint AuthorZhaolijiang

#include <stdio.h>
#include <stdlib.h>
#define MAXN 1010
int NUM[MAXN]; The current node can reach the minimum number of cloth.
int MAP[MAXN][MAXN];
int VIS[MAXN];
int NOW[MAXN]; Stack
int n,m,i;
void BFs (int k)
{
memset (vis,0,sizeof (VIS));
memset (Now,0,sizeof (now));
memset (num,1061109567,sizeof (num));
int ss = 0,ee = 0;
now[ss++] = k; K = = N
Vis[k] = 1;
Num[k] = 0;
while (Ss>ee)
{
int now = now[ee++];
for (i = 1;i <= n;i++)//traverse all reachable points of the point
{
if (map[now][i]==1&&vis[i]==0)
{
now[ss++] = i;
Vis[i] = 1;
Num[i] = num[i]<num[now]?num[i]:num[now]+1; Determine if it is the minimum value
}
}
}
if (num[1] = = 1061109567)//Determine if it can be reached
printf ("no\n");
Else
printf ("%d\n", num[1]);
}
int main ()
{
int u,v;
while (scanf ("%d%d", &n,&m)! = EOF)
{
memset (map,0,sizeof (map));
for (i = 0;i < m;i++)
{
scanf ("%d%d", &u,&v);
MAP[U][V] = 1;
}
BFS (n);
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.