#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int, int> pii;
int n,m;
int d[200000];
int queue[200000];
int q,r;
vector<int> v[200000];
int mark[200000];
int main()
{
scanf("%d %d",&n,&m);
int i, j, k;
for(i=0;i<n;i++)
d[i] = 1000000000;
queue[0] = 0;
q=0;r=1;
while(m--)
{
scanf("%d %d",&i,&j);
i--;j--;
v[i].push_back(j);
}
d[0] = 0;
while(q<r)
{
int qi = queue[q];
for(i=0;i<v[qi].size();i++)
{
j = v[qi][i];
if(d[j] > d[qi]+1)
{
d[j] = d[qi]+1;
queue[r] = j;
r++;
}
}
q++;
}
if(d[n-1] > 999999999)d[n-1] = -1;
printf("%d\n",d[n-1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8236 KB |
Output is correct |
2 |
Correct |
0 ms |
8236 KB |
Output is correct |
3 |
Correct |
0 ms |
8236 KB |
Output is correct |
4 |
Correct |
0 ms |
8236 KB |
Output is correct |
5 |
Correct |
0 ms |
8236 KB |
Output is correct |
6 |
Correct |
0 ms |
8236 KB |
Output is correct |
7 |
Correct |
0 ms |
8236 KB |
Output is correct |
8 |
Correct |
0 ms |
8236 KB |
Output is correct |
9 |
Correct |
0 ms |
8236 KB |
Output is correct |
10 |
Correct |
24 ms |
8772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8236 KB |
Output is correct |
2 |
Correct |
32 ms |
11272 KB |
Output is correct |
3 |
Correct |
56 ms |
11272 KB |
Output is correct |
4 |
Correct |
48 ms |
11272 KB |
Output is correct |
5 |
Correct |
56 ms |
11272 KB |
Output is correct |
6 |
Correct |
20 ms |
11272 KB |
Output is correct |
7 |
Correct |
40 ms |
11272 KB |
Output is correct |
8 |
Correct |
88 ms |
11404 KB |
Output is correct |
9 |
Correct |
44 ms |
11272 KB |
Output is correct |
10 |
Correct |
80 ms |
11008 KB |
Output is correct |