#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();
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;
}
# |
Verdict |
Execution time |
Memory |
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 |
168 ms |
7832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
6908 KB |
Output is correct |
2 |
Correct |
48 ms |
9944 KB |
Output is correct |
3 |
Execution timed out |
1000 ms |
60768 KB |
Program timed out |
4 |
Halted |
0 ms |
0 KB |
- |