#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
void solve(){
short n, m;
cin >> n >> m;
short b[m], p;
vector<short> h[n], sm[n];
const short s = 300;
for(int i = 0;i < m;i++){
cin >> b[i] >> p;
if(p>=s) h[b[i]].push_back(p);
else sm[b[i]].push_back(p);
}
int d[n][s];
bitset<s> v[n];
for(int i = 0;i < n;i++) for(int j = 0;j < s;j++) d[i][j] = INT_MAX, v[i][j] = false;
d[b[0]][0] = 0;
priority_queue<pair<int, pair<short, short>>,vector<pair<int, pair<short, short>>>, greater<pair<int, pair<short, short>>>> q;
q.push({0, {b[0], 0}});
while(!q.empty()){
short wi = q.top().second.first;
short po = q.top().second.second;
if(v[wi][po]) {
q.pop();
continue;
}
if(wi == b[1]){
cout << d[wi][po] << '\n';
return;
}
v[wi][po] = true;
q.pop();
if(po){
if(wi+po < n && d[wi+po][po] > 1+d[wi][po]){
d[wi+po][po] = 1+d[wi][po];
q.push({d[wi+po][po], {wi+po, po}});
if(1+d[wi][po] < d[wi+po][0]){
d[wi+po][0] = 1+d[wi][po];
q.push({d[wi+po][0], {wi+po, 0}});
}
}
if(wi-po >= 0 && d[wi-po][po] > 1+d[wi][po]){
d[wi-po][po] = 1+d[wi][po];
q.push({d[wi-po][po], {wi-po, po}});
if(1+d[wi][po] < d[wi-po][0]){
d[wi-po][0] = 1+d[wi][po];
q.push({d[wi-po][0], {wi-po, 0}});
}
}
continue;
}
#pragma GCC ivdep
for(int u : h[wi]){
for(int j = wi+u,c=1;j < n;j += u,c++) {
if(d[j][0] > d[wi][0]+c){
d[j][0] = d[wi][0]+c;
q.push({d[j][0], {j, 0}});
}
}
for(int j = wi-u,c=1;j >= 0;j -= u,c++) {
if(d[j][0] > d[wi][0]+c){
d[j][0] = d[wi][0]+c;
q.push({d[j][0], {j, 0}});
}
}
}
#pragma GCC ivdep
for(int po : sm[wi]){
if(v[wi][po]) continue;
d[wi][po] = d[wi][0];
if(wi+po < n && d[wi+po][po] > 1+d[wi][po]){
d[wi+po][po] = 1+d[wi][po];
q.push({d[wi+po][po], {wi+po, po}});
if(1+d[wi][po] < d[wi+po][0]){
d[wi+po][0] = 1+d[wi][po];
q.push({d[wi+po][0], {wi+po, 0}});
}
}
if(wi-po >= 0 && d[wi-po][po] > 1+d[wi][po]){
d[wi-po][po] = 1+d[wi][po];
q.push({d[wi-po][po], {wi-po, po}});
if(1+d[wi][po] < d[wi-po][0]){
d[wi-po][0] = 1+d[wi][po];
q.push({d[wi-po][0], {wi-po, 0}});
}
}
}
}
cout << "-1\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
456 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
636 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
712 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
3 ms |
2648 KB |
Output is correct |
19 |
Correct |
3 ms |
2760 KB |
Output is correct |
20 |
Correct |
3 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
2648 KB |
Output is correct |
23 |
Correct |
3 ms |
2504 KB |
Output is correct |
24 |
Correct |
3 ms |
2904 KB |
Output is correct |
25 |
Correct |
3 ms |
2904 KB |
Output is correct |
26 |
Correct |
3 ms |
2904 KB |
Output is correct |
27 |
Correct |
4 ms |
2908 KB |
Output is correct |
28 |
Correct |
4 ms |
2904 KB |
Output is correct |
29 |
Correct |
9 ms |
2908 KB |
Output is correct |
30 |
Correct |
4 ms |
2904 KB |
Output is correct |
31 |
Correct |
5 ms |
2904 KB |
Output is correct |
32 |
Correct |
5 ms |
2904 KB |
Output is correct |
33 |
Correct |
13 ms |
3044 KB |
Output is correct |
34 |
Correct |
10 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
820 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
3 ms |
2904 KB |
Output is correct |
20 |
Correct |
3 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
2 ms |
2648 KB |
Output is correct |
23 |
Correct |
3 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
3160 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2904 KB |
Output is correct |
27 |
Correct |
3 ms |
2904 KB |
Output is correct |
28 |
Correct |
3 ms |
3020 KB |
Output is correct |
29 |
Correct |
7 ms |
2904 KB |
Output is correct |
30 |
Correct |
4 ms |
2904 KB |
Output is correct |
31 |
Correct |
5 ms |
2904 KB |
Output is correct |
32 |
Correct |
4 ms |
2904 KB |
Output is correct |
33 |
Correct |
14 ms |
2904 KB |
Output is correct |
34 |
Correct |
9 ms |
2908 KB |
Output is correct |
35 |
Correct |
6 ms |
2652 KB |
Output is correct |
36 |
Correct |
3 ms |
1880 KB |
Output is correct |
37 |
Correct |
6 ms |
3416 KB |
Output is correct |
38 |
Correct |
7 ms |
3416 KB |
Output is correct |
39 |
Correct |
7 ms |
3416 KB |
Output is correct |
40 |
Correct |
7 ms |
3416 KB |
Output is correct |
41 |
Correct |
7 ms |
3416 KB |
Output is correct |
42 |
Correct |
5 ms |
3160 KB |
Output is correct |
43 |
Correct |
5 ms |
3160 KB |
Output is correct |
44 |
Correct |
6 ms |
3160 KB |
Output is correct |
45 |
Correct |
57 ms |
3672 KB |
Output is correct |
46 |
Correct |
37 ms |
3672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
1368 KB |
Output is correct |
18 |
Correct |
3 ms |
2648 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
2 ms |
2648 KB |
Output is correct |
23 |
Correct |
3 ms |
2392 KB |
Output is correct |
24 |
Correct |
3 ms |
2904 KB |
Output is correct |
25 |
Correct |
3 ms |
2904 KB |
Output is correct |
26 |
Correct |
3 ms |
2904 KB |
Output is correct |
27 |
Correct |
3 ms |
2904 KB |
Output is correct |
28 |
Correct |
3 ms |
2908 KB |
Output is correct |
29 |
Correct |
7 ms |
2904 KB |
Output is correct |
30 |
Correct |
4 ms |
2904 KB |
Output is correct |
31 |
Correct |
5 ms |
2904 KB |
Output is correct |
32 |
Correct |
4 ms |
2904 KB |
Output is correct |
33 |
Correct |
13 ms |
2904 KB |
Output is correct |
34 |
Correct |
8 ms |
2904 KB |
Output is correct |
35 |
Correct |
6 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
1884 KB |
Output is correct |
37 |
Correct |
6 ms |
3416 KB |
Output is correct |
38 |
Correct |
7 ms |
3420 KB |
Output is correct |
39 |
Correct |
7 ms |
3416 KB |
Output is correct |
40 |
Correct |
6 ms |
3420 KB |
Output is correct |
41 |
Correct |
7 ms |
3416 KB |
Output is correct |
42 |
Correct |
5 ms |
3160 KB |
Output is correct |
43 |
Correct |
6 ms |
3160 KB |
Output is correct |
44 |
Correct |
6 ms |
3160 KB |
Output is correct |
45 |
Correct |
57 ms |
3672 KB |
Output is correct |
46 |
Correct |
35 ms |
3676 KB |
Output is correct |
47 |
Correct |
22 ms |
16984 KB |
Output is correct |
48 |
Correct |
30 ms |
30440 KB |
Output is correct |
49 |
Correct |
33 ms |
33112 KB |
Output is correct |
50 |
Correct |
41 ms |
35920 KB |
Output is correct |
51 |
Correct |
48 ms |
39504 KB |
Output is correct |
52 |
Correct |
47 ms |
39504 KB |
Output is correct |
53 |
Correct |
40 ms |
39232 KB |
Output is correct |
54 |
Correct |
35 ms |
38232 KB |
Output is correct |
55 |
Correct |
36 ms |
38232 KB |
Output is correct |
56 |
Correct |
42 ms |
39256 KB |
Output is correct |
57 |
Correct |
38 ms |
38224 KB |
Output is correct |
58 |
Correct |
40 ms |
38540 KB |
Output is correct |
59 |
Correct |
41 ms |
38480 KB |
Output is correct |
60 |
Correct |
41 ms |
38480 KB |
Output is correct |
61 |
Correct |
40 ms |
38488 KB |
Output is correct |
62 |
Correct |
70 ms |
39512 KB |
Output is correct |
63 |
Correct |
161 ms |
39628 KB |
Output is correct |
64 |
Correct |
242 ms |
39628 KB |
Output is correct |
65 |
Correct |
584 ms |
39632 KB |
Output is correct |
66 |
Correct |
838 ms |
38992 KB |
Output is correct |
67 |
Correct |
483 ms |
38992 KB |
Output is correct |