#include <bits/stdc++.h>
using namespace std;
#define MAXN 30005
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
int N, M, S, E;
int D[MAXN][200];
bool V[MAXN][200];
vector <int> doge[MAXN];
int main()
{
scanf("%d%d", &N, &M);
for (int i=0;i<M;i++){
int b, p;
scanf("%d%d", &b, &p); doge[b].pb(p);
V[b][p] = 1;
if (!i) S = b;
if (i == 1) E = b;
}
for (int i=0;i<N;i++) for (int j=0;j<200;j++) D[i][j] = 2e9;
priority_queue <pii> que;
D[S][0] = 0;
que.push(mp(0, S*200));
while (!que.empty()){
int q = que.top().second, d = -que.top().first; que.pop();
int jmp = q%200; q /= 200;
if (D[q][jmp] != d) continue;
if (jmp){
if (D[q][0] > d)
D[q][0] = d, que.push(mp(-D[q][0], q*200));
if (q >= jmp && D[q-jmp][jmp] > d+1)
D[q-jmp][jmp] = d+1, que.push(mp(-d-1, (q-jmp)*200+jmp));
if (q+jmp < N && D[q+jmp][jmp] > d+1)
D[q+jmp][jmp] = d+1, que.push(mp(-d-1, (q+jmp)*200+jmp));
continue;
}
for (int p: doge[q]){
if (p > 199){
for (int i=q-p;i>=0;i-=p)
if (D[i][0] > d+(q-i)/p)
D[i][0] = d+(q-i)/p, que.push(mp(-D[i][0], i*200));
for (int i=q+p;i<N;i+=p)
if (D[i][0] > d+(i-q)/p)
D[i][0] = d+(i-q)/p, que.push(mp(-D[i][0], i*200));
}else{
if (D[q][p] > d)
D[q][p] = d, que.push(mp(-d, q*200+p));
}
}
}
printf("%d\n", D[E][0] < 2e9 ? D[E][0] : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
31724 KB |
Output is correct |
2 |
Correct |
0 ms |
31724 KB |
Output is correct |
3 |
Correct |
0 ms |
31724 KB |
Output is correct |
4 |
Correct |
0 ms |
31724 KB |
Output is correct |
5 |
Correct |
0 ms |
31724 KB |
Output is correct |
6 |
Correct |
0 ms |
31724 KB |
Output is correct |
7 |
Correct |
0 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
31724 KB |
Output is correct |
2 |
Correct |
0 ms |
31724 KB |
Output is correct |
3 |
Correct |
0 ms |
31724 KB |
Output is correct |
4 |
Correct |
0 ms |
31724 KB |
Output is correct |
5 |
Correct |
0 ms |
31724 KB |
Output is correct |
6 |
Correct |
0 ms |
31724 KB |
Output is correct |
7 |
Correct |
0 ms |
31724 KB |
Output is correct |
8 |
Correct |
0 ms |
31724 KB |
Output is correct |
9 |
Correct |
0 ms |
31724 KB |
Output is correct |
10 |
Correct |
0 ms |
31724 KB |
Output is correct |
11 |
Correct |
0 ms |
31724 KB |
Output is correct |
12 |
Correct |
0 ms |
31724 KB |
Output is correct |
13 |
Correct |
0 ms |
31724 KB |
Output is correct |
14 |
Correct |
0 ms |
31724 KB |
Output is correct |
15 |
Correct |
0 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
31724 KB |
Output is correct |
2 |
Correct |
0 ms |
31724 KB |
Output is correct |
3 |
Correct |
0 ms |
31724 KB |
Output is correct |
4 |
Correct |
0 ms |
31724 KB |
Output is correct |
5 |
Correct |
0 ms |
31724 KB |
Output is correct |
6 |
Correct |
0 ms |
31724 KB |
Output is correct |
7 |
Correct |
0 ms |
31724 KB |
Output is correct |
8 |
Correct |
0 ms |
31724 KB |
Output is correct |
9 |
Correct |
0 ms |
31724 KB |
Output is correct |
10 |
Correct |
1 ms |
31724 KB |
Output is correct |
11 |
Correct |
0 ms |
31724 KB |
Output is correct |
12 |
Correct |
2 ms |
31724 KB |
Output is correct |
13 |
Correct |
0 ms |
31724 KB |
Output is correct |
14 |
Correct |
0 ms |
31724 KB |
Output is correct |
15 |
Correct |
0 ms |
31724 KB |
Output is correct |
16 |
Correct |
0 ms |
31724 KB |
Output is correct |
17 |
Correct |
4 ms |
31724 KB |
Output is correct |
18 |
Correct |
0 ms |
31724 KB |
Output is correct |
19 |
Correct |
2 ms |
31724 KB |
Output is correct |
20 |
Correct |
0 ms |
31724 KB |
Output is correct |
21 |
Correct |
0 ms |
31724 KB |
Output is correct |
22 |
Correct |
0 ms |
31724 KB |
Output is correct |
23 |
Correct |
0 ms |
31724 KB |
Output is correct |
24 |
Correct |
0 ms |
31724 KB |
Output is correct |
25 |
Correct |
2 ms |
31724 KB |
Output is correct |
26 |
Correct |
0 ms |
31724 KB |
Output is correct |
27 |
Correct |
0 ms |
31724 KB |
Output is correct |
28 |
Correct |
3 ms |
31724 KB |
Output is correct |
29 |
Correct |
9 ms |
31724 KB |
Output is correct |
30 |
Correct |
4 ms |
31724 KB |
Output is correct |
31 |
Correct |
5 ms |
31724 KB |
Output is correct |
32 |
Correct |
0 ms |
31724 KB |
Output is correct |
33 |
Correct |
16 ms |
31724 KB |
Output is correct |
34 |
Correct |
12 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
31724 KB |
Output is correct |
2 |
Correct |
0 ms |
31724 KB |
Output is correct |
3 |
Correct |
0 ms |
31724 KB |
Output is correct |
4 |
Correct |
0 ms |
31724 KB |
Output is correct |
5 |
Correct |
0 ms |
31724 KB |
Output is correct |
6 |
Correct |
0 ms |
31724 KB |
Output is correct |
7 |
Correct |
0 ms |
31724 KB |
Output is correct |
8 |
Correct |
0 ms |
31724 KB |
Output is correct |
9 |
Correct |
0 ms |
31724 KB |
Output is correct |
10 |
Correct |
0 ms |
31724 KB |
Output is correct |
11 |
Correct |
3 ms |
31724 KB |
Output is correct |
12 |
Correct |
0 ms |
31724 KB |
Output is correct |
13 |
Correct |
0 ms |
31724 KB |
Output is correct |
14 |
Correct |
0 ms |
31724 KB |
Output is correct |
15 |
Correct |
0 ms |
31724 KB |
Output is correct |
16 |
Correct |
0 ms |
31724 KB |
Output is correct |
17 |
Correct |
0 ms |
31724 KB |
Output is correct |
18 |
Correct |
0 ms |
31724 KB |
Output is correct |
19 |
Correct |
0 ms |
31724 KB |
Output is correct |
20 |
Correct |
0 ms |
31724 KB |
Output is correct |
21 |
Correct |
0 ms |
31724 KB |
Output is correct |
22 |
Correct |
0 ms |
31724 KB |
Output is correct |
23 |
Correct |
0 ms |
31724 KB |
Output is correct |
24 |
Correct |
0 ms |
31724 KB |
Output is correct |
25 |
Correct |
0 ms |
31724 KB |
Output is correct |
26 |
Correct |
0 ms |
31724 KB |
Output is correct |
27 |
Correct |
0 ms |
31724 KB |
Output is correct |
28 |
Correct |
3 ms |
31724 KB |
Output is correct |
29 |
Correct |
9 ms |
31724 KB |
Output is correct |
30 |
Correct |
4 ms |
31724 KB |
Output is correct |
31 |
Correct |
3 ms |
31724 KB |
Output is correct |
32 |
Correct |
5 ms |
31724 KB |
Output is correct |
33 |
Correct |
16 ms |
31724 KB |
Output is correct |
34 |
Correct |
17 ms |
31724 KB |
Output is correct |
35 |
Correct |
13 ms |
31988 KB |
Output is correct |
36 |
Correct |
5 ms |
31724 KB |
Output is correct |
37 |
Correct |
27 ms |
32024 KB |
Output is correct |
38 |
Correct |
21 ms |
31988 KB |
Output is correct |
39 |
Correct |
20 ms |
31988 KB |
Output is correct |
40 |
Correct |
27 ms |
31988 KB |
Output is correct |
41 |
Correct |
24 ms |
31988 KB |
Output is correct |
42 |
Correct |
7 ms |
31856 KB |
Output is correct |
43 |
Correct |
0 ms |
31856 KB |
Output is correct |
44 |
Correct |
11 ms |
31856 KB |
Output is correct |
45 |
Correct |
66 ms |
32288 KB |
Output is correct |
46 |
Correct |
70 ms |
32284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
31724 KB |
Output is correct |
2 |
Correct |
0 ms |
31724 KB |
Output is correct |
3 |
Correct |
0 ms |
31724 KB |
Output is correct |
4 |
Correct |
0 ms |
31724 KB |
Output is correct |
5 |
Correct |
0 ms |
31724 KB |
Output is correct |
6 |
Correct |
0 ms |
31724 KB |
Output is correct |
7 |
Correct |
0 ms |
31724 KB |
Output is correct |
8 |
Correct |
0 ms |
31724 KB |
Output is correct |
9 |
Correct |
0 ms |
31724 KB |
Output is correct |
10 |
Correct |
0 ms |
31724 KB |
Output is correct |
11 |
Correct |
0 ms |
31724 KB |
Output is correct |
12 |
Correct |
0 ms |
31724 KB |
Output is correct |
13 |
Correct |
2 ms |
31724 KB |
Output is correct |
14 |
Correct |
2 ms |
31724 KB |
Output is correct |
15 |
Correct |
0 ms |
31724 KB |
Output is correct |
16 |
Correct |
0 ms |
31724 KB |
Output is correct |
17 |
Correct |
0 ms |
31724 KB |
Output is correct |
18 |
Correct |
0 ms |
31724 KB |
Output is correct |
19 |
Correct |
0 ms |
31724 KB |
Output is correct |
20 |
Correct |
0 ms |
31724 KB |
Output is correct |
21 |
Correct |
1 ms |
31724 KB |
Output is correct |
22 |
Correct |
2 ms |
31724 KB |
Output is correct |
23 |
Correct |
2 ms |
31724 KB |
Output is correct |
24 |
Correct |
0 ms |
31724 KB |
Output is correct |
25 |
Correct |
3 ms |
31724 KB |
Output is correct |
26 |
Correct |
0 ms |
31724 KB |
Output is correct |
27 |
Correct |
0 ms |
31724 KB |
Output is correct |
28 |
Correct |
0 ms |
31724 KB |
Output is correct |
29 |
Correct |
6 ms |
31724 KB |
Output is correct |
30 |
Correct |
0 ms |
31724 KB |
Output is correct |
31 |
Correct |
5 ms |
31724 KB |
Output is correct |
32 |
Correct |
4 ms |
31724 KB |
Output is correct |
33 |
Correct |
16 ms |
31724 KB |
Output is correct |
34 |
Correct |
12 ms |
31724 KB |
Output is correct |
35 |
Correct |
20 ms |
31988 KB |
Output is correct |
36 |
Correct |
5 ms |
31724 KB |
Output is correct |
37 |
Correct |
26 ms |
32024 KB |
Output is correct |
38 |
Correct |
27 ms |
31988 KB |
Output is correct |
39 |
Correct |
27 ms |
31988 KB |
Output is correct |
40 |
Correct |
22 ms |
31988 KB |
Output is correct |
41 |
Correct |
28 ms |
31988 KB |
Output is correct |
42 |
Correct |
0 ms |
31856 KB |
Output is correct |
43 |
Correct |
0 ms |
31856 KB |
Output is correct |
44 |
Correct |
0 ms |
31856 KB |
Output is correct |
45 |
Correct |
71 ms |
32288 KB |
Output is correct |
46 |
Correct |
71 ms |
32284 KB |
Output is correct |
47 |
Correct |
87 ms |
32456 KB |
Output is correct |
48 |
Correct |
11 ms |
32120 KB |
Output is correct |
49 |
Correct |
16 ms |
32120 KB |
Output is correct |
50 |
Correct |
11 ms |
31988 KB |
Output is correct |
51 |
Correct |
48 ms |
32700 KB |
Output is correct |
52 |
Correct |
52 ms |
32708 KB |
Output is correct |
53 |
Correct |
29 ms |
32252 KB |
Output is correct |
54 |
Correct |
3 ms |
31724 KB |
Output is correct |
55 |
Correct |
7 ms |
31724 KB |
Output is correct |
56 |
Correct |
15 ms |
32648 KB |
Output is correct |
57 |
Correct |
76 ms |
31724 KB |
Output is correct |
58 |
Correct |
21 ms |
31856 KB |
Output is correct |
59 |
Correct |
17 ms |
31856 KB |
Output is correct |
60 |
Correct |
18 ms |
31860 KB |
Output is correct |
61 |
Correct |
25 ms |
31872 KB |
Output is correct |
62 |
Correct |
39 ms |
32648 KB |
Output is correct |
63 |
Correct |
42 ms |
32736 KB |
Output is correct |
64 |
Correct |
46 ms |
32696 KB |
Output is correct |
65 |
Correct |
357 ms |
32708 KB |
Output is correct |
66 |
Execution timed out |
1000 ms |
33440 KB |
Program timed out |
67 |
Halted |
0 ms |
0 KB |
- |