#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define fillc(p, t, v) fill_n((t*) p, sizeof(p) / sizeof(t), v)
#define pb push_back
#define MID 269
#define O 30069
#define MAX 9690000000000ll
#define K 9999999999699ll
#define OM (O / MID + 69)
int N, M;
int B[O];
int P[O];
int TP;
vector<int> SMALL[O];
vector<int> BIG[O];
ll smallc[O][MID + 69];
bool seen1[O][MID + 69];
ll bigc[O][2 * OM + 69];
ll dp_small(int, int);
ll dp_big(int, int);
ll dp_small(int pos, int step) {
ll& ans = smallc[pos][step];
if(ans != -2) return ans;
ans = K;
ll a = ans;
if(pos == TP) return ans = 0;
for(int j : SMALL[pos]) {
a = min(a, dp_small(B[j], P[j]));
}
for(int j : BIG[pos]) {
a = min(a, dp_big(j, OM));
}
if(pos - step >= 0) {
a = min(a, 1 + dp_small(pos - step, step));
}
if(pos + step < N) {
a = min(a, 1 + dp_small(pos + step, step));
}
if(a == K) {
ans = -2;
return K;
}else{
ans = min(ans, MAX);
return ans = a;
}
}
ll dp_big(int i, int offset) {
ll& ans = bigc[i][offset];
if(ans != -2) return ans;
ans = MAX;
int pos = B[i] + (offset - OM) * P[i];
if(pos == TP) return ans = 0;
for(int j : SMALL[pos]) {
ans = min(ans, dp_small(B[j], P[j]));
}
for(int j : BIG[pos]) {
ans = min(ans, dp_big(j, OM));
}
if(pos - P[i] >= 0) {
ans = min(ans, 1 + dp_big(i, offset - 1));
}
if(pos + P[i] < N) {
ans = min(ans, 1 + dp_big(i, offset + 1));
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
for(int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
if(P[i] < MID)
SMALL[B[i]].pb(i);
else
BIG[B[i]].pb(i);
}
fillc(smallc, ll, MAX);
fillc(bigc, ll, MAX);
queue<pair<int, pair<int, int>>> q;
if(P[0] < MID) {
smallc[B[0]][P[0]] = 0;
q.push({0, {B[0], P[0]}});
}else{
bigc[0][OM] = 0;
q.push({1, {0, OM}});
}
while(not q.empty()) {
auto p = q.front();
q.pop();
int pos, step;
int a, b;
int t = p.first;
a = p.second.first;
b = p.second.second;
ll dis;
if (t == 0) {
dis = smallc[a][b];
pos = a;
step = b;
}else{
dis = bigc[a][b];
pos = B[a] + (b - OM) * P[a];
step = P[a];
}
for(int j : SMALL[pos]) {
ll& c = smallc[B[j]][P[j]];
if(dis < c) {
c = dis;
q.push({0, {B[j], P[j]}});
}
}
for(int j : BIG[pos]) {
ll& c = bigc[j][OM];
if(dis < c) {
c = dis;
q.push({1, {j, OM}});
}
}
if(t == 0) {
if(pos - step >= 0) {
ll& c = smallc[pos - step][step];
if(dis + 1 < c) {
c = dis + 1;
q.push({t, {pos - step, step}});
}
}
if(pos + step < N) {
ll& c = smallc[pos + step][step];
if(dis + 1 < c) {
c = dis + 1;
q.push({t, {pos + step, step}});
}
}
}else{
if(pos - step >= 0) {
ll& c = bigc[a][b - 1];
if(dis + 1 < c) {
c = dis + 1;
q.push({t, {a, b - 1}});
}
}
if(pos + step < N) {
ll& c = bigc[a][b + 1];
if(dis + 1 < c) {
c = dis + 1;
q.push({t, {a, b + 1}});
}
}
}
}
ll ans = min(smallc[B[1]][P[1]], bigc[1][OM]);
cout << (ans >= MAX ? -1 : ans) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
182312 KB |
Output is correct |
2 |
Correct |
96 ms |
182380 KB |
Output is correct |
3 |
Correct |
97 ms |
182252 KB |
Output is correct |
4 |
Correct |
96 ms |
182252 KB |
Output is correct |
5 |
Correct |
97 ms |
182252 KB |
Output is correct |
6 |
Correct |
97 ms |
182252 KB |
Output is correct |
7 |
Correct |
98 ms |
182252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
182340 KB |
Output is correct |
2 |
Correct |
97 ms |
182304 KB |
Output is correct |
3 |
Correct |
95 ms |
182252 KB |
Output is correct |
4 |
Correct |
96 ms |
182252 KB |
Output is correct |
5 |
Correct |
103 ms |
182240 KB |
Output is correct |
6 |
Correct |
98 ms |
182252 KB |
Output is correct |
7 |
Correct |
96 ms |
182252 KB |
Output is correct |
8 |
Correct |
96 ms |
182324 KB |
Output is correct |
9 |
Correct |
99 ms |
182380 KB |
Output is correct |
10 |
Correct |
96 ms |
182316 KB |
Output is correct |
11 |
Correct |
111 ms |
182380 KB |
Output is correct |
12 |
Correct |
95 ms |
182380 KB |
Output is correct |
13 |
Correct |
98 ms |
182380 KB |
Output is correct |
14 |
Correct |
100 ms |
182380 KB |
Output is correct |
15 |
Correct |
98 ms |
182380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
182252 KB |
Output is correct |
2 |
Correct |
96 ms |
182380 KB |
Output is correct |
3 |
Correct |
100 ms |
182380 KB |
Output is correct |
4 |
Correct |
96 ms |
182252 KB |
Output is correct |
5 |
Correct |
95 ms |
182252 KB |
Output is correct |
6 |
Correct |
96 ms |
182252 KB |
Output is correct |
7 |
Correct |
97 ms |
182252 KB |
Output is correct |
8 |
Correct |
96 ms |
182252 KB |
Output is correct |
9 |
Correct |
98 ms |
182400 KB |
Output is correct |
10 |
Correct |
98 ms |
182272 KB |
Output is correct |
11 |
Correct |
98 ms |
182380 KB |
Output is correct |
12 |
Correct |
96 ms |
182380 KB |
Output is correct |
13 |
Correct |
98 ms |
182380 KB |
Output is correct |
14 |
Correct |
111 ms |
182380 KB |
Output is correct |
15 |
Correct |
116 ms |
182380 KB |
Output is correct |
16 |
Correct |
98 ms |
182380 KB |
Output is correct |
17 |
Correct |
102 ms |
182380 KB |
Output is correct |
18 |
Correct |
102 ms |
182380 KB |
Output is correct |
19 |
Correct |
96 ms |
182380 KB |
Output is correct |
20 |
Correct |
110 ms |
182380 KB |
Output is correct |
21 |
Correct |
98 ms |
182252 KB |
Output is correct |
22 |
Correct |
96 ms |
182252 KB |
Output is correct |
23 |
Correct |
99 ms |
182380 KB |
Output is correct |
24 |
Correct |
101 ms |
182488 KB |
Output is correct |
25 |
Correct |
98 ms |
182380 KB |
Output is correct |
26 |
Correct |
97 ms |
182380 KB |
Output is correct |
27 |
Correct |
104 ms |
182508 KB |
Output is correct |
28 |
Correct |
110 ms |
182380 KB |
Output is correct |
29 |
Correct |
102 ms |
182252 KB |
Output is correct |
30 |
Correct |
97 ms |
182252 KB |
Output is correct |
31 |
Correct |
99 ms |
182380 KB |
Output is correct |
32 |
Correct |
102 ms |
182252 KB |
Output is correct |
33 |
Correct |
104 ms |
182380 KB |
Output is correct |
34 |
Correct |
103 ms |
182380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
182252 KB |
Output is correct |
2 |
Correct |
103 ms |
182252 KB |
Output is correct |
3 |
Correct |
98 ms |
182240 KB |
Output is correct |
4 |
Correct |
95 ms |
182252 KB |
Output is correct |
5 |
Correct |
97 ms |
182252 KB |
Output is correct |
6 |
Correct |
95 ms |
182252 KB |
Output is correct |
7 |
Correct |
98 ms |
182252 KB |
Output is correct |
8 |
Correct |
96 ms |
182252 KB |
Output is correct |
9 |
Correct |
97 ms |
182380 KB |
Output is correct |
10 |
Correct |
100 ms |
182380 KB |
Output is correct |
11 |
Correct |
96 ms |
182380 KB |
Output is correct |
12 |
Correct |
96 ms |
182380 KB |
Output is correct |
13 |
Correct |
99 ms |
182380 KB |
Output is correct |
14 |
Correct |
96 ms |
182380 KB |
Output is correct |
15 |
Correct |
98 ms |
182380 KB |
Output is correct |
16 |
Correct |
104 ms |
182252 KB |
Output is correct |
17 |
Correct |
101 ms |
182380 KB |
Output is correct |
18 |
Correct |
96 ms |
182308 KB |
Output is correct |
19 |
Correct |
97 ms |
182252 KB |
Output is correct |
20 |
Correct |
99 ms |
182420 KB |
Output is correct |
21 |
Correct |
98 ms |
182380 KB |
Output is correct |
22 |
Correct |
109 ms |
182380 KB |
Output is correct |
23 |
Correct |
100 ms |
182380 KB |
Output is correct |
24 |
Correct |
100 ms |
182380 KB |
Output is correct |
25 |
Correct |
97 ms |
182380 KB |
Output is correct |
26 |
Correct |
97 ms |
182380 KB |
Output is correct |
27 |
Correct |
99 ms |
182380 KB |
Output is correct |
28 |
Correct |
100 ms |
182380 KB |
Output is correct |
29 |
Correct |
99 ms |
182252 KB |
Output is correct |
30 |
Correct |
98 ms |
182252 KB |
Output is correct |
31 |
Correct |
99 ms |
182252 KB |
Output is correct |
32 |
Correct |
98 ms |
182272 KB |
Output is correct |
33 |
Correct |
110 ms |
182508 KB |
Output is correct |
34 |
Correct |
102 ms |
182380 KB |
Output is correct |
35 |
Correct |
140 ms |
182892 KB |
Output is correct |
36 |
Correct |
102 ms |
182380 KB |
Output is correct |
37 |
Correct |
140 ms |
182892 KB |
Output is correct |
38 |
Correct |
164 ms |
183532 KB |
Output is correct |
39 |
Correct |
154 ms |
183532 KB |
Output is correct |
40 |
Correct |
155 ms |
183404 KB |
Output is correct |
41 |
Correct |
165 ms |
183472 KB |
Output is correct |
42 |
Correct |
101 ms |
183148 KB |
Output is correct |
43 |
Correct |
109 ms |
183020 KB |
Output is correct |
44 |
Correct |
101 ms |
183148 KB |
Output is correct |
45 |
Correct |
157 ms |
183416 KB |
Output is correct |
46 |
Correct |
140 ms |
183276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
182252 KB |
Output is correct |
2 |
Correct |
97 ms |
182252 KB |
Output is correct |
3 |
Correct |
100 ms |
182252 KB |
Output is correct |
4 |
Correct |
107 ms |
182252 KB |
Output is correct |
5 |
Correct |
99 ms |
182252 KB |
Output is correct |
6 |
Correct |
95 ms |
182252 KB |
Output is correct |
7 |
Correct |
96 ms |
182252 KB |
Output is correct |
8 |
Correct |
95 ms |
182252 KB |
Output is correct |
9 |
Correct |
99 ms |
182252 KB |
Output is correct |
10 |
Correct |
97 ms |
182252 KB |
Output is correct |
11 |
Correct |
98 ms |
182380 KB |
Output is correct |
12 |
Correct |
96 ms |
182380 KB |
Output is correct |
13 |
Correct |
97 ms |
182380 KB |
Output is correct |
14 |
Correct |
97 ms |
182380 KB |
Output is correct |
15 |
Correct |
97 ms |
182380 KB |
Output is correct |
16 |
Correct |
97 ms |
182380 KB |
Output is correct |
17 |
Correct |
99 ms |
182380 KB |
Output is correct |
18 |
Correct |
100 ms |
182380 KB |
Output is correct |
19 |
Correct |
95 ms |
182252 KB |
Output is correct |
20 |
Correct |
97 ms |
182380 KB |
Output is correct |
21 |
Correct |
97 ms |
182360 KB |
Output is correct |
22 |
Correct |
99 ms |
182252 KB |
Output is correct |
23 |
Correct |
99 ms |
182380 KB |
Output is correct |
24 |
Correct |
112 ms |
182380 KB |
Output is correct |
25 |
Correct |
117 ms |
182380 KB |
Output is correct |
26 |
Correct |
98 ms |
182380 KB |
Output is correct |
27 |
Correct |
107 ms |
182380 KB |
Output is correct |
28 |
Correct |
101 ms |
182380 KB |
Output is correct |
29 |
Correct |
98 ms |
182252 KB |
Output is correct |
30 |
Correct |
96 ms |
182252 KB |
Output is correct |
31 |
Correct |
100 ms |
182380 KB |
Output is correct |
32 |
Correct |
99 ms |
182252 KB |
Output is correct |
33 |
Correct |
100 ms |
182316 KB |
Output is correct |
34 |
Correct |
106 ms |
182508 KB |
Output is correct |
35 |
Correct |
136 ms |
183020 KB |
Output is correct |
36 |
Correct |
98 ms |
182380 KB |
Output is correct |
37 |
Correct |
138 ms |
182892 KB |
Output is correct |
38 |
Correct |
164 ms |
183532 KB |
Output is correct |
39 |
Correct |
154 ms |
183524 KB |
Output is correct |
40 |
Correct |
157 ms |
183404 KB |
Output is correct |
41 |
Correct |
156 ms |
183404 KB |
Output is correct |
42 |
Correct |
101 ms |
183020 KB |
Output is correct |
43 |
Correct |
121 ms |
183020 KB |
Output is correct |
44 |
Correct |
101 ms |
183020 KB |
Output is correct |
45 |
Correct |
140 ms |
183276 KB |
Output is correct |
46 |
Correct |
140 ms |
183276 KB |
Output is correct |
47 |
Correct |
234 ms |
183532 KB |
Output is correct |
48 |
Correct |
109 ms |
183148 KB |
Output is correct |
49 |
Correct |
103 ms |
183148 KB |
Output is correct |
50 |
Correct |
100 ms |
182892 KB |
Output is correct |
51 |
Correct |
167 ms |
183532 KB |
Output is correct |
52 |
Correct |
193 ms |
183660 KB |
Output is correct |
53 |
Correct |
116 ms |
183532 KB |
Output is correct |
54 |
Correct |
98 ms |
182252 KB |
Output is correct |
55 |
Correct |
98 ms |
182380 KB |
Output is correct |
56 |
Correct |
105 ms |
183660 KB |
Output is correct |
57 |
Correct |
139 ms |
182360 KB |
Output is correct |
58 |
Correct |
106 ms |
182892 KB |
Output is correct |
59 |
Correct |
107 ms |
183020 KB |
Output is correct |
60 |
Correct |
114 ms |
183032 KB |
Output is correct |
61 |
Correct |
232 ms |
183020 KB |
Output is correct |
62 |
Correct |
198 ms |
183788 KB |
Output is correct |
63 |
Correct |
334 ms |
183168 KB |
Output is correct |
64 |
Correct |
473 ms |
183384 KB |
Output is correct |
65 |
Correct |
662 ms |
183420 KB |
Output is correct |
66 |
Correct |
704 ms |
183404 KB |
Output is correct |
67 |
Correct |
721 ms |
183372 KB |
Output is correct |