#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 200000;
int n, m, d[N];
bool vst[N];
vector<int> graph[N];
queue<int> q;
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
d[i] = 2*N;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
graph[u].push_back(v);
}
d[0] = 0;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
if (vst[u]) continue;
vst[u] = true;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
if (vst[v]) continue;
d[v] = min(d[v], d[u]+1);
q.push(v);
}
}
if (vst[n-1]) printf("%d\n", d[n-1]);
else printf("-1\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6908 KB |
Output is correct |
2 |
Correct |
0 ms |
6908 KB |
Output is correct |
3 |
Correct |
0 ms |
6908 KB |
Output is correct |
4 |
Correct |
0 ms |
6908 KB |
Output is correct |
5 |
Correct |
0 ms |
6908 KB |
Output is correct |
6 |
Correct |
0 ms |
6908 KB |
Output is correct |
7 |
Correct |
0 ms |
6908 KB |
Output is correct |
8 |
Correct |
0 ms |
6908 KB |
Output is correct |
9 |
Correct |
0 ms |
6908 KB |
Output is correct |
10 |
Correct |
16 ms |
7568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6908 KB |
Output is correct |
2 |
Correct |
44 ms |
9944 KB |
Output is correct |
3 |
Correct |
52 ms |
9944 KB |
Output is correct |
4 |
Correct |
40 ms |
9944 KB |
Output is correct |
5 |
Correct |
56 ms |
9944 KB |
Output is correct |
6 |
Correct |
36 ms |
9944 KB |
Output is correct |
7 |
Correct |
40 ms |
9944 KB |
Output is correct |
8 |
Correct |
88 ms |
10208 KB |
Output is correct |
9 |
Correct |
44 ms |
9944 KB |
Output is correct |
10 |
Correct |
72 ms |
9680 KB |
Output is correct |