#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <climits>
using namespace std;
#define LIM 150
int B[30000], P[30000];
set<int> A[30000];
int D[30000][LIM + 1];
set<pair<int, pair<int, int> > > Q;
void update(int i, int j, int d) {
if(D[i][j] != INT_MAX/2) {
Q.erase({D[i][j], {i, j}});
}
D[i][j] = d;
Q.insert({D[i][j], {i, j}});
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int N, M; cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
A[B[i]].insert(P[i]);
}
for(int i = 0; i < N; i++) {
for(int j = 0; j <= LIM; j++) {
D[i][j] = INT_MAX/2;
}
}
int ans = -1;
Q.insert({0, {B[0], LIM}}); D[B[0]][LIM] = 0;
while(!Q.empty()) {
auto it = *Q.begin(); Q.erase(Q.begin());
int i = it.second.first, j = it.second.second;
if(i == B[1]) {
ans = it.first;
break;
}
if(j == LIM) {
for(int x: A[i]) {
if(x < LIM) {
if(D[i][j] < D[i][x]) {
update(i, x, D[i][j]);
}
}
else {
for(int d = 1; i + d * x < N; d++) {
if(A[i + d * x].size() == 0) continue;
if(D[i][j] + d < D[i + d * x][LIM]) {
update(i + d * x, LIM, D[i][j] + d);
}
// if(A[i + d * x].count(x)) break;
}
for(int d = 1; i - d * x >= 0; d++) {
if(A[i - d * x].size() == 0) continue;
if(D[i][j] + d < D[i - d * x][LIM]) {
update(i - d * x, LIM, D[i][j] + d);
}
// if(A[i - d * x].count(x)) break;
}
}
}
}
else {
if(D[i][j] < D[i][LIM]) {
update(i, LIM, D[i][j]);
}
if(i + j < N && D[i][j] + 1 < D[i + j][j]) {
update(i + j, j, D[i][j] + 1);
}
if(i - j >= 0 && D[i][j] + 1 < D[i - j][j]) {
update(i - j, j, D[i][j] + 1);
}
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
21520 KB |
Output is correct |
2 |
Correct |
0 ms |
21520 KB |
Output is correct |
3 |
Correct |
0 ms |
21520 KB |
Output is correct |
4 |
Correct |
0 ms |
21520 KB |
Output is correct |
5 |
Correct |
0 ms |
21520 KB |
Output is correct |
6 |
Correct |
0 ms |
21520 KB |
Output is correct |
7 |
Correct |
0 ms |
21520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
21520 KB |
Output is correct |
2 |
Correct |
0 ms |
21520 KB |
Output is correct |
3 |
Correct |
0 ms |
21520 KB |
Output is correct |
4 |
Correct |
0 ms |
21520 KB |
Output is correct |
5 |
Correct |
0 ms |
21520 KB |
Output is correct |
6 |
Correct |
0 ms |
21520 KB |
Output is correct |
7 |
Correct |
0 ms |
21520 KB |
Output is correct |
8 |
Correct |
0 ms |
21520 KB |
Output is correct |
9 |
Correct |
0 ms |
21520 KB |
Output is correct |
10 |
Correct |
0 ms |
21520 KB |
Output is correct |
11 |
Correct |
0 ms |
21520 KB |
Output is correct |
12 |
Correct |
0 ms |
21520 KB |
Output is correct |
13 |
Correct |
0 ms |
21520 KB |
Output is correct |
14 |
Correct |
0 ms |
21652 KB |
Output is correct |
15 |
Correct |
3 ms |
21652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
21520 KB |
Output is correct |
2 |
Correct |
0 ms |
21520 KB |
Output is correct |
3 |
Correct |
0 ms |
21520 KB |
Output is correct |
4 |
Correct |
0 ms |
21520 KB |
Output is correct |
5 |
Correct |
0 ms |
21520 KB |
Output is correct |
6 |
Correct |
0 ms |
21520 KB |
Output is correct |
7 |
Correct |
0 ms |
21520 KB |
Output is correct |
8 |
Correct |
0 ms |
21520 KB |
Output is correct |
9 |
Correct |
0 ms |
21520 KB |
Output is correct |
10 |
Correct |
0 ms |
21520 KB |
Output is correct |
11 |
Correct |
0 ms |
21520 KB |
Output is correct |
12 |
Correct |
0 ms |
21520 KB |
Output is correct |
13 |
Correct |
0 ms |
21520 KB |
Output is correct |
14 |
Correct |
3 ms |
21652 KB |
Output is correct |
15 |
Correct |
3 ms |
21652 KB |
Output is correct |
16 |
Correct |
0 ms |
21520 KB |
Output is correct |
17 |
Correct |
0 ms |
21652 KB |
Output is correct |
18 |
Correct |
0 ms |
21520 KB |
Output is correct |
19 |
Correct |
0 ms |
21520 KB |
Output is correct |
20 |
Correct |
0 ms |
21520 KB |
Output is correct |
21 |
Correct |
0 ms |
21520 KB |
Output is correct |
22 |
Correct |
0 ms |
21520 KB |
Output is correct |
23 |
Correct |
0 ms |
21520 KB |
Output is correct |
24 |
Correct |
0 ms |
21652 KB |
Output is correct |
25 |
Correct |
0 ms |
21520 KB |
Output is correct |
26 |
Correct |
3 ms |
21520 KB |
Output is correct |
27 |
Correct |
0 ms |
21520 KB |
Output is correct |
28 |
Correct |
3 ms |
21520 KB |
Output is correct |
29 |
Correct |
13 ms |
21520 KB |
Output is correct |
30 |
Correct |
3 ms |
21520 KB |
Output is correct |
31 |
Correct |
6 ms |
21520 KB |
Output is correct |
32 |
Correct |
6 ms |
21520 KB |
Output is correct |
33 |
Correct |
19 ms |
21652 KB |
Output is correct |
34 |
Correct |
13 ms |
21652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
21520 KB |
Output is correct |
2 |
Correct |
0 ms |
21520 KB |
Output is correct |
3 |
Correct |
0 ms |
21520 KB |
Output is correct |
4 |
Correct |
0 ms |
21520 KB |
Output is correct |
5 |
Correct |
0 ms |
21520 KB |
Output is correct |
6 |
Correct |
0 ms |
21520 KB |
Output is correct |
7 |
Correct |
0 ms |
21520 KB |
Output is correct |
8 |
Correct |
0 ms |
21520 KB |
Output is correct |
9 |
Correct |
0 ms |
21520 KB |
Output is correct |
10 |
Correct |
0 ms |
21520 KB |
Output is correct |
11 |
Correct |
0 ms |
21520 KB |
Output is correct |
12 |
Correct |
0 ms |
21520 KB |
Output is correct |
13 |
Correct |
0 ms |
21520 KB |
Output is correct |
14 |
Correct |
3 ms |
21652 KB |
Output is correct |
15 |
Correct |
3 ms |
21652 KB |
Output is correct |
16 |
Correct |
0 ms |
21520 KB |
Output is correct |
17 |
Correct |
0 ms |
21652 KB |
Output is correct |
18 |
Correct |
0 ms |
21520 KB |
Output is correct |
19 |
Correct |
0 ms |
21520 KB |
Output is correct |
20 |
Correct |
0 ms |
21520 KB |
Output is correct |
21 |
Correct |
0 ms |
21520 KB |
Output is correct |
22 |
Correct |
0 ms |
21520 KB |
Output is correct |
23 |
Correct |
0 ms |
21520 KB |
Output is correct |
24 |
Correct |
3 ms |
21652 KB |
Output is correct |
25 |
Correct |
0 ms |
21520 KB |
Output is correct |
26 |
Correct |
0 ms |
21520 KB |
Output is correct |
27 |
Correct |
0 ms |
21520 KB |
Output is correct |
28 |
Correct |
3 ms |
21520 KB |
Output is correct |
29 |
Correct |
9 ms |
21520 KB |
Output is correct |
30 |
Correct |
3 ms |
21520 KB |
Output is correct |
31 |
Correct |
6 ms |
21520 KB |
Output is correct |
32 |
Correct |
3 ms |
21520 KB |
Output is correct |
33 |
Correct |
16 ms |
21652 KB |
Output is correct |
34 |
Correct |
9 ms |
21652 KB |
Output is correct |
35 |
Correct |
6 ms |
22840 KB |
Output is correct |
36 |
Correct |
0 ms |
21784 KB |
Output is correct |
37 |
Correct |
13 ms |
22708 KB |
Output is correct |
38 |
Correct |
16 ms |
23104 KB |
Output is correct |
39 |
Correct |
16 ms |
22840 KB |
Output is correct |
40 |
Correct |
16 ms |
22972 KB |
Output is correct |
41 |
Correct |
16 ms |
22972 KB |
Output is correct |
42 |
Correct |
6 ms |
21520 KB |
Output is correct |
43 |
Correct |
6 ms |
21520 KB |
Output is correct |
44 |
Correct |
6 ms |
21520 KB |
Output is correct |
45 |
Correct |
76 ms |
23632 KB |
Output is correct |
46 |
Correct |
63 ms |
23632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
21520 KB |
Output is correct |
2 |
Correct |
0 ms |
21520 KB |
Output is correct |
3 |
Correct |
0 ms |
21520 KB |
Output is correct |
4 |
Correct |
0 ms |
21520 KB |
Output is correct |
5 |
Correct |
0 ms |
21520 KB |
Output is correct |
6 |
Correct |
0 ms |
21520 KB |
Output is correct |
7 |
Correct |
0 ms |
21520 KB |
Output is correct |
8 |
Correct |
0 ms |
21520 KB |
Output is correct |
9 |
Correct |
0 ms |
21520 KB |
Output is correct |
10 |
Correct |
0 ms |
21520 KB |
Output is correct |
11 |
Correct |
0 ms |
21520 KB |
Output is correct |
12 |
Correct |
0 ms |
21520 KB |
Output is correct |
13 |
Correct |
0 ms |
21520 KB |
Output is correct |
14 |
Correct |
3 ms |
21652 KB |
Output is correct |
15 |
Correct |
0 ms |
21652 KB |
Output is correct |
16 |
Correct |
0 ms |
21520 KB |
Output is correct |
17 |
Correct |
0 ms |
21652 KB |
Output is correct |
18 |
Correct |
0 ms |
21520 KB |
Output is correct |
19 |
Correct |
0 ms |
21520 KB |
Output is correct |
20 |
Correct |
0 ms |
21520 KB |
Output is correct |
21 |
Correct |
0 ms |
21520 KB |
Output is correct |
22 |
Correct |
0 ms |
21520 KB |
Output is correct |
23 |
Correct |
0 ms |
21520 KB |
Output is correct |
24 |
Correct |
3 ms |
21652 KB |
Output is correct |
25 |
Correct |
3 ms |
21520 KB |
Output is correct |
26 |
Correct |
3 ms |
21520 KB |
Output is correct |
27 |
Correct |
0 ms |
21520 KB |
Output is correct |
28 |
Correct |
3 ms |
21520 KB |
Output is correct |
29 |
Correct |
9 ms |
21520 KB |
Output is correct |
30 |
Correct |
3 ms |
21520 KB |
Output is correct |
31 |
Correct |
3 ms |
21520 KB |
Output is correct |
32 |
Correct |
3 ms |
21520 KB |
Output is correct |
33 |
Correct |
33 ms |
21652 KB |
Output is correct |
34 |
Correct |
13 ms |
21652 KB |
Output is correct |
35 |
Correct |
13 ms |
22840 KB |
Output is correct |
36 |
Correct |
0 ms |
21784 KB |
Output is correct |
37 |
Correct |
6 ms |
22708 KB |
Output is correct |
38 |
Correct |
23 ms |
23104 KB |
Output is correct |
39 |
Correct |
13 ms |
22840 KB |
Output is correct |
40 |
Correct |
13 ms |
22972 KB |
Output is correct |
41 |
Correct |
13 ms |
22972 KB |
Output is correct |
42 |
Correct |
6 ms |
21520 KB |
Output is correct |
43 |
Correct |
6 ms |
21520 KB |
Output is correct |
44 |
Correct |
6 ms |
21520 KB |
Output is correct |
45 |
Correct |
76 ms |
23632 KB |
Output is correct |
46 |
Correct |
46 ms |
23632 KB |
Output is correct |
47 |
Correct |
29 ms |
23236 KB |
Output is correct |
48 |
Correct |
13 ms |
22576 KB |
Output is correct |
49 |
Correct |
9 ms |
22444 KB |
Output is correct |
50 |
Correct |
13 ms |
22180 KB |
Output is correct |
51 |
Correct |
33 ms |
23368 KB |
Output is correct |
52 |
Correct |
43 ms |
23368 KB |
Output is correct |
53 |
Correct |
19 ms |
22840 KB |
Output is correct |
54 |
Correct |
9 ms |
21520 KB |
Output is correct |
55 |
Correct |
9 ms |
21520 KB |
Output is correct |
56 |
Correct |
19 ms |
22840 KB |
Output is correct |
57 |
Correct |
6 ms |
21652 KB |
Output is correct |
58 |
Correct |
29 ms |
21520 KB |
Output is correct |
59 |
Correct |
23 ms |
21520 KB |
Output is correct |
60 |
Correct |
23 ms |
21520 KB |
Output is correct |
61 |
Correct |
16 ms |
21652 KB |
Output is correct |
62 |
Correct |
49 ms |
22840 KB |
Output is correct |
63 |
Correct |
53 ms |
22972 KB |
Output is correct |
64 |
Correct |
39 ms |
22840 KB |
Output is correct |
65 |
Correct |
223 ms |
23104 KB |
Output is correct |
66 |
Correct |
953 ms |
23632 KB |
Output is correct |
67 |
Correct |
313 ms |
23632 KB |
Output is correct |