#include <bits/stdc++.h>
using namespace std;
int n, m; const int SQ=10;
vector <int> pos [30010];
pair <int, int> l [30010];
int sol [30010][SQ+10];
int bio [30010][SQ+10];
typedef pair < int, pair <int, int> > byk;
priority_queue < byk, vector <byk>, greater <byk> > s;
int main(){
ios_base::sync_with_stdio (false);
cin.tie (0);
cin >> n >> m;
for (int i=0; i<m; i++) cin >> l [i].first >> l [i].second;
for (int i=0; i<m; i++) pos [l [i].first].push_back (l [i].second);
for (int i=0; i<n; i++){
for (int j=0; j<SQ+1; j++){
sol [i][j]=1e9;
s.push ({1e9, {i, j}});
}
}
s.push ({0, {l [0].first, l [0].second}});
sol [l [0].first][l [0].second]=0;
while (!s.empty ()){
byk tp = s.top();
int res=tp.first;
if (res==1e9) break;
int x=tp.second.first;
int p=tp.second.second;
s.pop();
//cout << res << " " << x << " " << p << endl;
bio [x][p]=1;
//cout << res << " " << x << " " << p << endl;
int siz = pos[x].size();
for (int i=0; i<siz; i++){
int po=pos [x][i];
if (po>SQ){
for (int j=x+po; j<n; j+=po){
if (sol [j][0]<=(j-x)/po+res || bio [j][0]) continue;
s.push ({sol [j][0]=(j-x)/po+res, {j, 0}});
}
for (int j=x-po; j>=0; j-=po){
if (sol [j][0]<=(x-j)/po+res || bio [j][0]) continue;
s.push ({sol [j][0]=(x-j)/po+res, {j, 0}});
}
}
else {
if (x-po>=0){
if (sol [x-po][po]>1+res && !bio [x-po][po]) {
sol [x-po][po]=1+res;
s.push ({sol [x-po][po], {x-po, po}});
}
}
if (x+po<n){
if (sol [x+po][po]>1+res && !bio [x+po][po]) {
sol [x+po][po]=1+res;
s.push ({sol [x+po][po], {x+po, po}});
}
}
}
}
if (x-p>=0 && p!=0){
if (sol [x-p][p]>1+res && !bio [x-p][p]) {
sol [x-p][p]=1+res;
s.push ({sol [x-p][p], {x-p, p}});
}
}
if (x+p<n && p!=0){
if (sol [x+p][p]>1+res && !bio [x+p][p]) {
sol [x+p][p]=1+res;
s.push ({sol [x+p][p], {x+p, p}});
}
}
}
int so=1e9;
for (int i=0; i<SQ+1; i++) so=min (so, sol [l [1].first][i]);
if (so==1e9) cout << -1;
else cout << so;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1028 KB |
Output is correct |
2 |
Correct |
1 ms |
1032 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1032 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1036 KB |
Output is correct |
15 |
Correct |
1 ms |
1036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1032 KB |
Output is correct |
8 |
Correct |
1 ms |
1032 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1044 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1164 KB |
Output is correct |
17 |
Correct |
2 ms |
1428 KB |
Output is correct |
18 |
Correct |
2 ms |
1684 KB |
Output is correct |
19 |
Correct |
2 ms |
1624 KB |
Output is correct |
20 |
Correct |
2 ms |
1744 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1616 KB |
Output is correct |
23 |
Correct |
2 ms |
1684 KB |
Output is correct |
24 |
Correct |
3 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
1808 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1684 KB |
Output is correct |
28 |
Correct |
3 ms |
1804 KB |
Output is correct |
29 |
Correct |
4 ms |
1872 KB |
Output is correct |
30 |
Correct |
2 ms |
1744 KB |
Output is correct |
31 |
Correct |
2 ms |
1748 KB |
Output is correct |
32 |
Correct |
2 ms |
1744 KB |
Output is correct |
33 |
Correct |
9 ms |
1872 KB |
Output is correct |
34 |
Correct |
9 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1032 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1028 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1032 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1040 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1160 KB |
Output is correct |
17 |
Correct |
3 ms |
1428 KB |
Output is correct |
18 |
Correct |
1 ms |
1684 KB |
Output is correct |
19 |
Correct |
1 ms |
1684 KB |
Output is correct |
20 |
Correct |
2 ms |
1744 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1684 KB |
Output is correct |
23 |
Correct |
2 ms |
1684 KB |
Output is correct |
24 |
Correct |
3 ms |
1748 KB |
Output is correct |
25 |
Correct |
3 ms |
1872 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1748 KB |
Output is correct |
28 |
Correct |
3 ms |
1744 KB |
Output is correct |
29 |
Correct |
4 ms |
1744 KB |
Output is correct |
30 |
Correct |
2 ms |
1744 KB |
Output is correct |
31 |
Correct |
2 ms |
1764 KB |
Output is correct |
32 |
Correct |
2 ms |
1788 KB |
Output is correct |
33 |
Correct |
9 ms |
1880 KB |
Output is correct |
34 |
Correct |
10 ms |
1876 KB |
Output is correct |
35 |
Correct |
13 ms |
2196 KB |
Output is correct |
36 |
Correct |
3 ms |
1556 KB |
Output is correct |
37 |
Correct |
17 ms |
2180 KB |
Output is correct |
38 |
Correct |
17 ms |
2388 KB |
Output is correct |
39 |
Correct |
17 ms |
2308 KB |
Output is correct |
40 |
Correct |
17 ms |
2388 KB |
Output is correct |
41 |
Correct |
20 ms |
2388 KB |
Output is correct |
42 |
Correct |
5 ms |
2320 KB |
Output is correct |
43 |
Correct |
5 ms |
2320 KB |
Output is correct |
44 |
Correct |
5 ms |
2336 KB |
Output is correct |
45 |
Correct |
26 ms |
2384 KB |
Output is correct |
46 |
Correct |
26 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1036 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1040 KB |
Output is correct |
13 |
Correct |
1 ms |
1040 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1164 KB |
Output is correct |
17 |
Correct |
3 ms |
1428 KB |
Output is correct |
18 |
Correct |
1 ms |
1684 KB |
Output is correct |
19 |
Correct |
1 ms |
1684 KB |
Output is correct |
20 |
Correct |
2 ms |
1744 KB |
Output is correct |
21 |
Correct |
1 ms |
1172 KB |
Output is correct |
22 |
Correct |
1 ms |
1684 KB |
Output is correct |
23 |
Correct |
2 ms |
1680 KB |
Output is correct |
24 |
Correct |
3 ms |
1748 KB |
Output is correct |
25 |
Correct |
2 ms |
1744 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1684 KB |
Output is correct |
28 |
Correct |
3 ms |
1744 KB |
Output is correct |
29 |
Correct |
4 ms |
1744 KB |
Output is correct |
30 |
Correct |
2 ms |
1744 KB |
Output is correct |
31 |
Correct |
2 ms |
1796 KB |
Output is correct |
32 |
Correct |
2 ms |
1744 KB |
Output is correct |
33 |
Correct |
10 ms |
1872 KB |
Output is correct |
34 |
Correct |
9 ms |
1812 KB |
Output is correct |
35 |
Correct |
12 ms |
2196 KB |
Output is correct |
36 |
Correct |
3 ms |
1556 KB |
Output is correct |
37 |
Correct |
17 ms |
2256 KB |
Output is correct |
38 |
Correct |
17 ms |
2392 KB |
Output is correct |
39 |
Correct |
17 ms |
2388 KB |
Output is correct |
40 |
Correct |
17 ms |
2388 KB |
Output is correct |
41 |
Correct |
16 ms |
2324 KB |
Output is correct |
42 |
Correct |
5 ms |
2320 KB |
Output is correct |
43 |
Correct |
5 ms |
2320 KB |
Output is correct |
44 |
Correct |
4 ms |
2304 KB |
Output is correct |
45 |
Correct |
26 ms |
2280 KB |
Output is correct |
46 |
Correct |
26 ms |
2340 KB |
Output is correct |
47 |
Correct |
67 ms |
5832 KB |
Output is correct |
48 |
Correct |
10 ms |
6660 KB |
Output is correct |
49 |
Correct |
12 ms |
10052 KB |
Output is correct |
50 |
Correct |
11 ms |
9804 KB |
Output is correct |
51 |
Correct |
53 ms |
11004 KB |
Output is correct |
52 |
Correct |
46 ms |
11072 KB |
Output is correct |
53 |
Correct |
22 ms |
10640 KB |
Output is correct |
54 |
Correct |
11 ms |
9724 KB |
Output is correct |
55 |
Correct |
12 ms |
9720 KB |
Output is correct |
56 |
Correct |
18 ms |
10892 KB |
Output is correct |
57 |
Correct |
198 ms |
11120 KB |
Output is correct |
58 |
Correct |
20 ms |
10240 KB |
Output is correct |
59 |
Correct |
24 ms |
10212 KB |
Output is correct |
60 |
Correct |
25 ms |
10176 KB |
Output is correct |
61 |
Correct |
23 ms |
10152 KB |
Output is correct |
62 |
Correct |
40 ms |
10856 KB |
Output is correct |
63 |
Correct |
53 ms |
11296 KB |
Output is correct |
64 |
Correct |
55 ms |
11388 KB |
Output is correct |
65 |
Correct |
74 ms |
11664 KB |
Output is correct |
66 |
Correct |
431 ms |
12196 KB |
Output is correct |
67 |
Correct |
424 ms |
12296 KB |
Output is correct |