#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 30010;
int n, m, b[M], p[M], dist[N];
bool mark[N], exist[N][N];
int main(){
cin >> n >> m;
for (int i = 0; i < m; i ++){
cin >> b[i] >> p[i];
exist[b[i]][p[i]] = 1;
}
memset(dist, 63, sizeof dist);
dist[b[0]] = 0;
for (int ii = 0; ii < n; ii ++){
int v = -1;
for (int j = 0; j < n; j ++)
if (!mark[j] and (v == -1 or dist[v] > dist[j]))
v = j;
mark[v] = 1;
for (int d = 1; d <= n; d ++){
if (!exist[v][d]) continue;
for (int i = 1; v + d * i < n; i ++)
dist[v + d * i] = min(dist[v + d * i], dist[v] + i);
for (int i = 1; v - d * i >= 0; i ++)
dist[v - d * i] = min(dist[v - d * i], dist[v] + i);
}
}
if (dist[b[1]] > 1e9)
dist[b[1]] = -1;
cout << dist[b[1]] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
8 ms |
3548 KB |
Output is correct |
19 |
Correct |
9 ms |
3160 KB |
Output is correct |
20 |
Correct |
12 ms |
4188 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
7 ms |
3164 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
8 ms |
3928 KB |
Output is correct |
25 |
Correct |
9 ms |
4048 KB |
Output is correct |
26 |
Correct |
8 ms |
2652 KB |
Output is correct |
27 |
Correct |
9 ms |
2652 KB |
Output is correct |
28 |
Correct |
9 ms |
4432 KB |
Output is correct |
29 |
Correct |
9 ms |
348 KB |
Output is correct |
30 |
Correct |
9 ms |
532 KB |
Output is correct |
31 |
Correct |
9 ms |
348 KB |
Output is correct |
32 |
Correct |
9 ms |
348 KB |
Output is correct |
33 |
Correct |
11 ms |
604 KB |
Output is correct |
34 |
Correct |
9 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2716 KB |
Output is correct |
12 |
Correct |
1 ms |
2504 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
2500 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2508 KB |
Output is correct |
18 |
Correct |
7 ms |
3420 KB |
Output is correct |
19 |
Correct |
9 ms |
3284 KB |
Output is correct |
20 |
Correct |
12 ms |
4188 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
7 ms |
3320 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
8 ms |
3932 KB |
Output is correct |
25 |
Correct |
9 ms |
4188 KB |
Output is correct |
26 |
Correct |
9 ms |
2508 KB |
Output is correct |
27 |
Correct |
8 ms |
2652 KB |
Output is correct |
28 |
Correct |
11 ms |
4188 KB |
Output is correct |
29 |
Correct |
9 ms |
560 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
9 ms |
344 KB |
Output is correct |
32 |
Correct |
9 ms |
568 KB |
Output is correct |
33 |
Correct |
9 ms |
616 KB |
Output is correct |
34 |
Correct |
9 ms |
604 KB |
Output is correct |
35 |
Correct |
12 ms |
3672 KB |
Output is correct |
36 |
Correct |
4 ms |
2736 KB |
Output is correct |
37 |
Correct |
14 ms |
4580 KB |
Output is correct |
38 |
Correct |
18 ms |
4880 KB |
Output is correct |
39 |
Correct |
18 ms |
4696 KB |
Output is correct |
40 |
Correct |
18 ms |
4812 KB |
Output is correct |
41 |
Correct |
35 ms |
4852 KB |
Output is correct |
42 |
Correct |
15 ms |
2908 KB |
Output is correct |
43 |
Correct |
15 ms |
2904 KB |
Output is correct |
44 |
Correct |
23 ms |
4812 KB |
Output is correct |
45 |
Correct |
17 ms |
2908 KB |
Output is correct |
46 |
Correct |
16 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
2512 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
7 ms |
3404 KB |
Output is correct |
19 |
Correct |
9 ms |
3160 KB |
Output is correct |
20 |
Correct |
13 ms |
4420 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
8 ms |
3300 KB |
Output is correct |
23 |
Correct |
6 ms |
3164 KB |
Output is correct |
24 |
Correct |
8 ms |
3928 KB |
Output is correct |
25 |
Correct |
9 ms |
4188 KB |
Output is correct |
26 |
Correct |
9 ms |
2652 KB |
Output is correct |
27 |
Correct |
8 ms |
2680 KB |
Output is correct |
28 |
Correct |
9 ms |
4184 KB |
Output is correct |
29 |
Correct |
9 ms |
452 KB |
Output is correct |
30 |
Correct |
13 ms |
348 KB |
Output is correct |
31 |
Correct |
9 ms |
348 KB |
Output is correct |
32 |
Correct |
9 ms |
568 KB |
Output is correct |
33 |
Correct |
9 ms |
600 KB |
Output is correct |
34 |
Correct |
10 ms |
604 KB |
Output is correct |
35 |
Correct |
12 ms |
3704 KB |
Output is correct |
36 |
Correct |
4 ms |
2652 KB |
Output is correct |
37 |
Correct |
13 ms |
4444 KB |
Output is correct |
38 |
Correct |
18 ms |
4848 KB |
Output is correct |
39 |
Correct |
17 ms |
4700 KB |
Output is correct |
40 |
Correct |
17 ms |
4696 KB |
Output is correct |
41 |
Correct |
18 ms |
4824 KB |
Output is correct |
42 |
Correct |
14 ms |
2908 KB |
Output is correct |
43 |
Correct |
15 ms |
2908 KB |
Output is correct |
44 |
Correct |
18 ms |
4756 KB |
Output is correct |
45 |
Correct |
16 ms |
3132 KB |
Output is correct |
46 |
Correct |
16 ms |
2908 KB |
Output is correct |
47 |
Runtime error |
0 ms |
604 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |