#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 = 800;
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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
800 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
5 ms |
5980 KB |
Output is correct |
19 |
Correct |
6 ms |
7004 KB |
Output is correct |
20 |
Correct |
6 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
6 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
5724 KB |
Output is correct |
24 |
Correct |
9 ms |
6748 KB |
Output is correct |
25 |
Correct |
7 ms |
7004 KB |
Output is correct |
26 |
Correct |
7 ms |
7004 KB |
Output is correct |
27 |
Correct |
7 ms |
7004 KB |
Output is correct |
28 |
Correct |
7 ms |
6864 KB |
Output is correct |
29 |
Correct |
11 ms |
6948 KB |
Output is correct |
30 |
Correct |
8 ms |
7004 KB |
Output is correct |
31 |
Correct |
10 ms |
7004 KB |
Output is correct |
32 |
Correct |
8 ms |
7004 KB |
Output is correct |
33 |
Correct |
17 ms |
7004 KB |
Output is correct |
34 |
Correct |
14 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
6 ms |
5980 KB |
Output is correct |
19 |
Correct |
6 ms |
7004 KB |
Output is correct |
20 |
Correct |
7 ms |
6996 KB |
Output is correct |
21 |
Correct |
2 ms |
1880 KB |
Output is correct |
22 |
Correct |
7 ms |
6568 KB |
Output is correct |
23 |
Correct |
6 ms |
5720 KB |
Output is correct |
24 |
Correct |
7 ms |
6748 KB |
Output is correct |
25 |
Correct |
7 ms |
7004 KB |
Output is correct |
26 |
Correct |
8 ms |
7004 KB |
Output is correct |
27 |
Correct |
7 ms |
7020 KB |
Output is correct |
28 |
Correct |
8 ms |
7004 KB |
Output is correct |
29 |
Correct |
11 ms |
7004 KB |
Output is correct |
30 |
Correct |
7 ms |
7004 KB |
Output is correct |
31 |
Correct |
9 ms |
7004 KB |
Output is correct |
32 |
Correct |
10 ms |
7004 KB |
Output is correct |
33 |
Correct |
17 ms |
7004 KB |
Output is correct |
34 |
Correct |
13 ms |
7000 KB |
Output is correct |
35 |
Correct |
14 ms |
5468 KB |
Output is correct |
36 |
Correct |
5 ms |
4112 KB |
Output is correct |
37 |
Correct |
12 ms |
7236 KB |
Output is correct |
38 |
Correct |
12 ms |
7256 KB |
Output is correct |
39 |
Correct |
11 ms |
7004 KB |
Output is correct |
40 |
Correct |
10 ms |
7256 KB |
Output is correct |
41 |
Correct |
10 ms |
7260 KB |
Output is correct |
42 |
Correct |
9 ms |
7004 KB |
Output is correct |
43 |
Correct |
9 ms |
7004 KB |
Output is correct |
44 |
Correct |
9 ms |
7004 KB |
Output is correct |
45 |
Correct |
66 ms |
7516 KB |
Output is correct |
46 |
Correct |
39 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 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 |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
1096 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
6 ms |
6184 KB |
Output is correct |
19 |
Correct |
6 ms |
7004 KB |
Output is correct |
20 |
Correct |
7 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
6 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
5724 KB |
Output is correct |
24 |
Correct |
7 ms |
6556 KB |
Output is correct |
25 |
Correct |
7 ms |
7004 KB |
Output is correct |
26 |
Correct |
8 ms |
7004 KB |
Output is correct |
27 |
Correct |
7 ms |
7004 KB |
Output is correct |
28 |
Correct |
7 ms |
7004 KB |
Output is correct |
29 |
Correct |
13 ms |
7000 KB |
Output is correct |
30 |
Correct |
8 ms |
7004 KB |
Output is correct |
31 |
Correct |
9 ms |
7000 KB |
Output is correct |
32 |
Correct |
9 ms |
7004 KB |
Output is correct |
33 |
Correct |
17 ms |
7000 KB |
Output is correct |
34 |
Correct |
12 ms |
7004 KB |
Output is correct |
35 |
Correct |
9 ms |
5468 KB |
Output is correct |
36 |
Correct |
4 ms |
3932 KB |
Output is correct |
37 |
Correct |
10 ms |
7240 KB |
Output is correct |
38 |
Correct |
12 ms |
7256 KB |
Output is correct |
39 |
Correct |
10 ms |
7260 KB |
Output is correct |
40 |
Correct |
10 ms |
7248 KB |
Output is correct |
41 |
Correct |
12 ms |
7148 KB |
Output is correct |
42 |
Correct |
10 ms |
7004 KB |
Output is correct |
43 |
Correct |
11 ms |
7004 KB |
Output is correct |
44 |
Correct |
9 ms |
7000 KB |
Output is correct |
45 |
Correct |
64 ms |
7516 KB |
Output is correct |
46 |
Correct |
41 ms |
7516 KB |
Output is correct |
47 |
Correct |
49 ms |
41816 KB |
Output is correct |
48 |
Correct |
94 ms |
77100 KB |
Output is correct |
49 |
Correct |
93 ms |
84164 KB |
Output is correct |
50 |
Correct |
92 ms |
92368 KB |
Output is correct |
51 |
Correct |
110 ms |
99552 KB |
Output is correct |
52 |
Correct |
116 ms |
99556 KB |
Output is correct |
53 |
Correct |
108 ms |
99476 KB |
Output is correct |
54 |
Correct |
99 ms |
98820 KB |
Output is correct |
55 |
Correct |
100 ms |
98816 KB |
Output is correct |
56 |
Correct |
105 ms |
99584 KB |
Output is correct |
57 |
Correct |
95 ms |
98892 KB |
Output is correct |
58 |
Correct |
101 ms |
99016 KB |
Output is correct |
59 |
Correct |
107 ms |
99012 KB |
Output is correct |
60 |
Correct |
109 ms |
98920 KB |
Output is correct |
61 |
Correct |
103 ms |
98772 KB |
Output is correct |
62 |
Correct |
151 ms |
99920 KB |
Output is correct |
63 |
Correct |
603 ms |
99284 KB |
Output is correct |
64 |
Correct |
662 ms |
99248 KB |
Output is correct |
65 |
Correct |
858 ms |
99820 KB |
Output is correct |
66 |
Correct |
977 ms |
99344 KB |
Output is correct |
67 |
Correct |
556 ms |
99348 KB |
Output is correct |