#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];
set <int> A[200][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);
if (!i) S = b;
if (i == 1) E = b;
}
for (int i=0;i<N;i++) D[i] = 2e9;
priority_queue <pii> que;
D[S] = 0;
que.push(mp(0, S));
for (int i=0;i<N;i++) for (int j=1;j<200;j++) A[j][i%j].insert(i);
while (!que.empty()){
int q = que.top().second, d = -que.top().first; que.pop();
if (D[q] != d) continue;
if (q == E) break;
for (int i=1;i<200;i++) A[i][q%i].erase(q);
for (int p: doge[q]){
if (p > 199){
for (int i=q-p;i>=0;i-=p)
if (D[i] > D[q]+(q-i)/p)
D[i] = D[q]+(q-i)/p, que.push(mp(-D[i], i));
for (int i=q+p;i<N;i+=p)
if (D[i] > D[q]+(i-q)/p)
D[i] = D[q]+(i-q)/p, que.push(mp(-D[i], i));
}else{
int m = q % p;
for (auto it=A[p][m].begin();it!=A[p][m].end();it++){
if (D[*it] > D[q]+abs(*it-q)/p)
D[*it] = D[q]+abs(*it-q)/p, que.push(mp(-D[*it], *it));
}
}
}
}
printf("%d\n", D[E] < 2e9 ? D[E] : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Correct |
0 ms |
4420 KB |
Output is correct |
4 |
Correct |
0 ms |
4420 KB |
Output is correct |
5 |
Correct |
2 ms |
4420 KB |
Output is correct |
6 |
Correct |
0 ms |
4420 KB |
Output is correct |
7 |
Correct |
0 ms |
4420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Correct |
0 ms |
4420 KB |
Output is correct |
4 |
Correct |
1 ms |
4420 KB |
Output is correct |
5 |
Correct |
0 ms |
4420 KB |
Output is correct |
6 |
Correct |
0 ms |
4420 KB |
Output is correct |
7 |
Correct |
0 ms |
4420 KB |
Output is correct |
8 |
Correct |
0 ms |
4684 KB |
Output is correct |
9 |
Correct |
0 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
5344 KB |
Output is correct |
11 |
Correct |
0 ms |
5344 KB |
Output is correct |
12 |
Correct |
2 ms |
5344 KB |
Output is correct |
13 |
Correct |
4 ms |
5344 KB |
Output is correct |
14 |
Correct |
2 ms |
5344 KB |
Output is correct |
15 |
Correct |
2 ms |
5344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Correct |
0 ms |
4420 KB |
Output is correct |
4 |
Correct |
0 ms |
4420 KB |
Output is correct |
5 |
Correct |
0 ms |
4420 KB |
Output is correct |
6 |
Correct |
0 ms |
4420 KB |
Output is correct |
7 |
Correct |
0 ms |
4420 KB |
Output is correct |
8 |
Correct |
0 ms |
4684 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
5344 KB |
Output is correct |
11 |
Correct |
4 ms |
5344 KB |
Output is correct |
12 |
Correct |
3 ms |
5344 KB |
Output is correct |
13 |
Correct |
0 ms |
5344 KB |
Output is correct |
14 |
Correct |
0 ms |
5344 KB |
Output is correct |
15 |
Correct |
4 ms |
5344 KB |
Output is correct |
16 |
Correct |
7 ms |
6268 KB |
Output is correct |
17 |
Correct |
48 ms |
11284 KB |
Output is correct |
18 |
Correct |
120 ms |
20656 KB |
Output is correct |
19 |
Correct |
159 ms |
23032 KB |
Output is correct |
20 |
Correct |
258 ms |
23164 KB |
Output is correct |
21 |
Correct |
7 ms |
7984 KB |
Output is correct |
22 |
Correct |
119 ms |
21052 KB |
Output is correct |
23 |
Correct |
150 ms |
19336 KB |
Output is correct |
24 |
Correct |
211 ms |
22108 KB |
Output is correct |
25 |
Correct |
168 ms |
23032 KB |
Output is correct |
26 |
Correct |
217 ms |
23032 KB |
Output is correct |
27 |
Correct |
205 ms |
23032 KB |
Output is correct |
28 |
Correct |
205 ms |
23032 KB |
Output is correct |
29 |
Correct |
187 ms |
23200 KB |
Output is correct |
30 |
Correct |
213 ms |
23032 KB |
Output is correct |
31 |
Correct |
201 ms |
23032 KB |
Output is correct |
32 |
Correct |
207 ms |
23032 KB |
Output is correct |
33 |
Correct |
217 ms |
23208 KB |
Output is correct |
34 |
Correct |
206 ms |
23204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Correct |
0 ms |
4420 KB |
Output is correct |
4 |
Correct |
0 ms |
4420 KB |
Output is correct |
5 |
Correct |
0 ms |
4420 KB |
Output is correct |
6 |
Correct |
0 ms |
4420 KB |
Output is correct |
7 |
Correct |
0 ms |
4420 KB |
Output is correct |
8 |
Correct |
0 ms |
4684 KB |
Output is correct |
9 |
Correct |
0 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
5344 KB |
Output is correct |
11 |
Correct |
0 ms |
5344 KB |
Output is correct |
12 |
Correct |
5 ms |
5344 KB |
Output is correct |
13 |
Correct |
0 ms |
5344 KB |
Output is correct |
14 |
Correct |
3 ms |
5344 KB |
Output is correct |
15 |
Correct |
0 ms |
5344 KB |
Output is correct |
16 |
Correct |
7 ms |
6268 KB |
Output is correct |
17 |
Correct |
48 ms |
11284 KB |
Output is correct |
18 |
Correct |
122 ms |
20656 KB |
Output is correct |
19 |
Correct |
166 ms |
23032 KB |
Output is correct |
20 |
Correct |
271 ms |
23164 KB |
Output is correct |
21 |
Correct |
16 ms |
7984 KB |
Output is correct |
22 |
Correct |
128 ms |
21052 KB |
Output is correct |
23 |
Correct |
144 ms |
19336 KB |
Output is correct |
24 |
Correct |
219 ms |
22108 KB |
Output is correct |
25 |
Correct |
181 ms |
23032 KB |
Output is correct |
26 |
Correct |
213 ms |
23032 KB |
Output is correct |
27 |
Correct |
211 ms |
23032 KB |
Output is correct |
28 |
Correct |
220 ms |
23032 KB |
Output is correct |
29 |
Correct |
203 ms |
23200 KB |
Output is correct |
30 |
Correct |
198 ms |
23032 KB |
Output is correct |
31 |
Correct |
212 ms |
23032 KB |
Output is correct |
32 |
Correct |
200 ms |
23032 KB |
Output is correct |
33 |
Correct |
209 ms |
23208 KB |
Output is correct |
34 |
Correct |
215 ms |
23204 KB |
Output is correct |
35 |
Correct |
164 ms |
18016 KB |
Output is correct |
36 |
Correct |
80 ms |
14584 KB |
Output is correct |
37 |
Correct |
212 ms |
22636 KB |
Output is correct |
38 |
Correct |
245 ms |
23300 KB |
Output is correct |
39 |
Correct |
155 ms |
23296 KB |
Output is correct |
40 |
Correct |
188 ms |
23308 KB |
Output is correct |
41 |
Correct |
253 ms |
23296 KB |
Output is correct |
42 |
Correct |
231 ms |
23164 KB |
Output is correct |
43 |
Correct |
223 ms |
23164 KB |
Output is correct |
44 |
Correct |
263 ms |
23296 KB |
Output is correct |
45 |
Correct |
222 ms |
23304 KB |
Output is correct |
46 |
Correct |
236 ms |
23304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Correct |
0 ms |
4420 KB |
Output is correct |
4 |
Correct |
0 ms |
4420 KB |
Output is correct |
5 |
Correct |
0 ms |
4420 KB |
Output is correct |
6 |
Correct |
0 ms |
4420 KB |
Output is correct |
7 |
Correct |
0 ms |
4420 KB |
Output is correct |
8 |
Correct |
0 ms |
4684 KB |
Output is correct |
9 |
Correct |
0 ms |
4948 KB |
Output is correct |
10 |
Correct |
0 ms |
5344 KB |
Output is correct |
11 |
Correct |
4 ms |
5344 KB |
Output is correct |
12 |
Correct |
5 ms |
5344 KB |
Output is correct |
13 |
Correct |
4 ms |
5344 KB |
Output is correct |
14 |
Correct |
4 ms |
5344 KB |
Output is correct |
15 |
Correct |
4 ms |
5344 KB |
Output is correct |
16 |
Correct |
7 ms |
6268 KB |
Output is correct |
17 |
Correct |
50 ms |
11284 KB |
Output is correct |
18 |
Correct |
132 ms |
20656 KB |
Output is correct |
19 |
Correct |
154 ms |
23032 KB |
Output is correct |
20 |
Correct |
279 ms |
23164 KB |
Output is correct |
21 |
Correct |
10 ms |
7984 KB |
Output is correct |
22 |
Correct |
136 ms |
21052 KB |
Output is correct |
23 |
Correct |
150 ms |
19336 KB |
Output is correct |
24 |
Correct |
225 ms |
22108 KB |
Output is correct |
25 |
Correct |
178 ms |
23032 KB |
Output is correct |
26 |
Correct |
205 ms |
23032 KB |
Output is correct |
27 |
Correct |
220 ms |
23032 KB |
Output is correct |
28 |
Correct |
220 ms |
23032 KB |
Output is correct |
29 |
Correct |
201 ms |
23200 KB |
Output is correct |
30 |
Correct |
236 ms |
23032 KB |
Output is correct |
31 |
Correct |
211 ms |
23032 KB |
Output is correct |
32 |
Correct |
195 ms |
23032 KB |
Output is correct |
33 |
Correct |
208 ms |
23208 KB |
Output is correct |
34 |
Correct |
201 ms |
23204 KB |
Output is correct |
35 |
Correct |
141 ms |
18016 KB |
Output is correct |
36 |
Correct |
73 ms |
14584 KB |
Output is correct |
37 |
Correct |
201 ms |
22636 KB |
Output is correct |
38 |
Correct |
244 ms |
23300 KB |
Output is correct |
39 |
Correct |
161 ms |
23296 KB |
Output is correct |
40 |
Correct |
183 ms |
23308 KB |
Output is correct |
41 |
Correct |
232 ms |
23296 KB |
Output is correct |
42 |
Correct |
222 ms |
23164 KB |
Output is correct |
43 |
Correct |
218 ms |
23164 KB |
Output is correct |
44 |
Correct |
289 ms |
23296 KB |
Output is correct |
45 |
Correct |
255 ms |
23304 KB |
Output is correct |
46 |
Correct |
229 ms |
23304 KB |
Output is correct |
47 |
Execution timed out |
1000 ms |
120308 KB |
Program timed out |
48 |
Halted |
0 ms |
0 KB |
- |