#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e4 + 30, B = 200;
const int inf = 2e9;
int n, m;
int dist[N][B];
bool bio[N];
vector<int> adj[N];
struct rec {
int d, v, p;
friend bool operator<(const rec& a, const rec& b) {
if (a.d != b.d)
return a.d < b.d;
if (a.v != b.v)
return a.v < b.v;
return a.p < b.p;
}
};
/*
5 3
0 2
1 1
4 1
*/
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 0; i < N; i++) {
for(int j = 0; j < B; j++) {
dist[i][j] = inf;
}
}
cin >> n >> m;
int s = 0, f = 0;
for(int i = 0; i < m; i++) {
int x, k;
cin >> x >> k;
adj[x].push_back(k);
if(i == 0) s = x;
if(i == 1) f = x;
}
// priority_queue<array<int,3>> q;
priority_queue<rec> q;
for(auto x : adj[s]) {
if(x < B) {
dist[s][x] = 0;
q.push({dist[s][x], s, x});
} else {
for(int k = 1; s + k * x < N; k++) {
dist[s + k * x][0] = k;
q.push({-dist[s + k * x][0], s + k * x, 0});
}
for(int k = 1; s - k * x >= 0; k++) {
dist[s - k * x][0] = k;
q.push({-dist[s - k * x][0], s - k * x, 0});
}
}
}
bio[s] = 1;
while(!q.empty()) {
auto top = q.top();
int d = top.d;
int v = top.v;
int p = top.p;
q.pop();
d = -d;
if(d > dist[v][p]) continue;
if(!bio[v]) {
for(auto x : adj[v]) {
if(x < B) {
dist[v][x] = d;
q.push({-dist[v][x], v, x});
} else {
for(int k = 1; v + k * x < n; k++) {
if(dist[v + k * x][0] > d + k) {
dist[v + k * x][0] = d + k;
q.push({-dist[v + k * x][0], v + k * x, 0});
}
}
for(int k = 1; v - k * x >= 0; k++) {
if(dist[v - k * x][0] > d + k) {
dist[v - k * x][0] = d + k;
q.push({-dist[v - k * x][0], v - k * x, 0});
}
}
}
}
bio[v] = 1;
}
if(v + p < N && dist[v + p][p] > d + 1) {
dist[v + p][p] = d + 1;
q.push({-dist[v + p][p], v + p, p});
}
if(v - p >= 0 && dist[v - p][p] > d + 1) {
dist[v - p][p] = d + 1;
q.push({-dist[v - p][p], v - p, p});
}
}
int ans = inf;
for(int k = 0; k < B; k++)
ans = min(ans, dist[f][k]);
if(ans == inf)
ans = -1;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24532 KB |
Output is correct |
2 |
Correct |
11 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24460 KB |
Output is correct |
4 |
Correct |
10 ms |
24532 KB |
Output is correct |
5 |
Correct |
13 ms |
24520 KB |
Output is correct |
6 |
Correct |
14 ms |
24512 KB |
Output is correct |
7 |
Correct |
12 ms |
24464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24496 KB |
Output is correct |
2 |
Correct |
14 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
13 ms |
24528 KB |
Output is correct |
5 |
Correct |
13 ms |
24532 KB |
Output is correct |
6 |
Correct |
18 ms |
24560 KB |
Output is correct |
7 |
Correct |
12 ms |
24532 KB |
Output is correct |
8 |
Correct |
14 ms |
24520 KB |
Output is correct |
9 |
Correct |
20 ms |
24536 KB |
Output is correct |
10 |
Correct |
50 ms |
24496 KB |
Output is correct |
11 |
Correct |
115 ms |
24640 KB |
Output is correct |
12 |
Correct |
28 ms |
24532 KB |
Output is correct |
13 |
Correct |
15 ms |
24504 KB |
Output is correct |
14 |
Correct |
139 ms |
24604 KB |
Output is correct |
15 |
Correct |
139 ms |
24532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24456 KB |
Output is correct |
2 |
Correct |
11 ms |
24556 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
11 ms |
24532 KB |
Output is correct |
5 |
Correct |
13 ms |
24484 KB |
Output is correct |
6 |
Correct |
13 ms |
24532 KB |
Output is correct |
7 |
Correct |
13 ms |
24544 KB |
Output is correct |
8 |
Correct |
15 ms |
24508 KB |
Output is correct |
9 |
Correct |
20 ms |
24564 KB |
Output is correct |
10 |
Correct |
57 ms |
24572 KB |
Output is correct |
11 |
Correct |
115 ms |
24648 KB |
Output is correct |
12 |
Correct |
19 ms |
24500 KB |
Output is correct |
13 |
Correct |
12 ms |
24580 KB |
Output is correct |
14 |
Correct |
131 ms |
24604 KB |
Output is correct |
15 |
Correct |
134 ms |
24532 KB |
Output is correct |
16 |
Correct |
11 ms |
24444 KB |
Output is correct |
17 |
Correct |
64 ms |
24612 KB |
Output is correct |
18 |
Correct |
14 ms |
24464 KB |
Output is correct |
19 |
Correct |
13 ms |
24532 KB |
Output is correct |
20 |
Correct |
14 ms |
24624 KB |
Output is correct |
21 |
Correct |
11 ms |
24532 KB |
Output is correct |
22 |
Correct |
11 ms |
24532 KB |
Output is correct |
23 |
Correct |
19 ms |
24532 KB |
Output is correct |
24 |
Correct |
26 ms |
24544 KB |
Output is correct |
25 |
Correct |
15 ms |
24500 KB |
Output is correct |
26 |
Correct |
21 ms |
24544 KB |
Output is correct |
27 |
Correct |
18 ms |
24572 KB |
Output is correct |
28 |
Correct |
39 ms |
24528 KB |
Output is correct |
29 |
Correct |
78 ms |
24700 KB |
Output is correct |
30 |
Correct |
26 ms |
24540 KB |
Output is correct |
31 |
Correct |
44 ms |
24520 KB |
Output is correct |
32 |
Correct |
34 ms |
24576 KB |
Output is correct |
33 |
Correct |
132 ms |
24604 KB |
Output is correct |
34 |
Correct |
143 ms |
24600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24556 KB |
Output is correct |
2 |
Correct |
12 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24532 KB |
Output is correct |
4 |
Correct |
10 ms |
24448 KB |
Output is correct |
5 |
Correct |
12 ms |
24532 KB |
Output is correct |
6 |
Correct |
13 ms |
24532 KB |
Output is correct |
7 |
Correct |
11 ms |
24532 KB |
Output is correct |
8 |
Correct |
14 ms |
24560 KB |
Output is correct |
9 |
Correct |
20 ms |
24560 KB |
Output is correct |
10 |
Correct |
53 ms |
24532 KB |
Output is correct |
11 |
Correct |
120 ms |
24644 KB |
Output is correct |
12 |
Correct |
18 ms |
24624 KB |
Output is correct |
13 |
Correct |
12 ms |
24612 KB |
Output is correct |
14 |
Correct |
134 ms |
24604 KB |
Output is correct |
15 |
Correct |
133 ms |
24524 KB |
Output is correct |
16 |
Correct |
11 ms |
24468 KB |
Output is correct |
17 |
Correct |
63 ms |
24612 KB |
Output is correct |
18 |
Correct |
11 ms |
24488 KB |
Output is correct |
19 |
Correct |
12 ms |
24540 KB |
Output is correct |
20 |
Correct |
12 ms |
24532 KB |
Output is correct |
21 |
Correct |
12 ms |
24536 KB |
Output is correct |
22 |
Correct |
11 ms |
24532 KB |
Output is correct |
23 |
Correct |
20 ms |
24592 KB |
Output is correct |
24 |
Correct |
28 ms |
24548 KB |
Output is correct |
25 |
Correct |
15 ms |
24592 KB |
Output is correct |
26 |
Correct |
21 ms |
24508 KB |
Output is correct |
27 |
Correct |
18 ms |
24532 KB |
Output is correct |
28 |
Correct |
40 ms |
24556 KB |
Output is correct |
29 |
Correct |
70 ms |
24576 KB |
Output is correct |
30 |
Correct |
28 ms |
24468 KB |
Output is correct |
31 |
Correct |
52 ms |
24560 KB |
Output is correct |
32 |
Correct |
36 ms |
24560 KB |
Output is correct |
33 |
Correct |
144 ms |
24596 KB |
Output is correct |
34 |
Correct |
135 ms |
24596 KB |
Output is correct |
35 |
Correct |
144 ms |
25008 KB |
Output is correct |
36 |
Correct |
43 ms |
24544 KB |
Output is correct |
37 |
Correct |
171 ms |
24980 KB |
Output is correct |
38 |
Correct |
144 ms |
24892 KB |
Output is correct |
39 |
Correct |
151 ms |
24876 KB |
Output is correct |
40 |
Correct |
145 ms |
24952 KB |
Output is correct |
41 |
Correct |
144 ms |
24916 KB |
Output is correct |
42 |
Correct |
25 ms |
25232 KB |
Output is correct |
43 |
Correct |
22 ms |
25276 KB |
Output is correct |
44 |
Correct |
19 ms |
25276 KB |
Output is correct |
45 |
Correct |
584 ms |
25340 KB |
Output is correct |
46 |
Correct |
591 ms |
25196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
24516 KB |
Output is correct |
2 |
Correct |
11 ms |
24532 KB |
Output is correct |
3 |
Correct |
11 ms |
24540 KB |
Output is correct |
4 |
Correct |
11 ms |
24488 KB |
Output is correct |
5 |
Correct |
12 ms |
24532 KB |
Output is correct |
6 |
Correct |
16 ms |
24468 KB |
Output is correct |
7 |
Correct |
12 ms |
24532 KB |
Output is correct |
8 |
Correct |
15 ms |
24564 KB |
Output is correct |
9 |
Correct |
19 ms |
24532 KB |
Output is correct |
10 |
Correct |
49 ms |
24560 KB |
Output is correct |
11 |
Correct |
114 ms |
24640 KB |
Output is correct |
12 |
Correct |
20 ms |
24624 KB |
Output is correct |
13 |
Correct |
12 ms |
24532 KB |
Output is correct |
14 |
Correct |
135 ms |
24608 KB |
Output is correct |
15 |
Correct |
139 ms |
24608 KB |
Output is correct |
16 |
Correct |
11 ms |
24532 KB |
Output is correct |
17 |
Correct |
65 ms |
24604 KB |
Output is correct |
18 |
Correct |
11 ms |
24580 KB |
Output is correct |
19 |
Correct |
11 ms |
24528 KB |
Output is correct |
20 |
Correct |
12 ms |
24532 KB |
Output is correct |
21 |
Correct |
11 ms |
24572 KB |
Output is correct |
22 |
Correct |
12 ms |
24576 KB |
Output is correct |
23 |
Correct |
21 ms |
24592 KB |
Output is correct |
24 |
Correct |
25 ms |
24564 KB |
Output is correct |
25 |
Correct |
14 ms |
24532 KB |
Output is correct |
26 |
Correct |
20 ms |
24532 KB |
Output is correct |
27 |
Correct |
17 ms |
24628 KB |
Output is correct |
28 |
Correct |
33 ms |
24532 KB |
Output is correct |
29 |
Correct |
71 ms |
24572 KB |
Output is correct |
30 |
Correct |
28 ms |
24532 KB |
Output is correct |
31 |
Correct |
46 ms |
24584 KB |
Output is correct |
32 |
Correct |
34 ms |
24560 KB |
Output is correct |
33 |
Correct |
140 ms |
24752 KB |
Output is correct |
34 |
Correct |
132 ms |
24604 KB |
Output is correct |
35 |
Correct |
145 ms |
24860 KB |
Output is correct |
36 |
Correct |
48 ms |
24532 KB |
Output is correct |
37 |
Correct |
175 ms |
24984 KB |
Output is correct |
38 |
Correct |
143 ms |
24976 KB |
Output is correct |
39 |
Correct |
144 ms |
24948 KB |
Output is correct |
40 |
Correct |
150 ms |
24872 KB |
Output is correct |
41 |
Correct |
143 ms |
24872 KB |
Output is correct |
42 |
Correct |
26 ms |
25300 KB |
Output is correct |
43 |
Correct |
21 ms |
25264 KB |
Output is correct |
44 |
Correct |
16 ms |
25284 KB |
Output is correct |
45 |
Correct |
588 ms |
25264 KB |
Output is correct |
46 |
Correct |
582 ms |
25236 KB |
Output is correct |
47 |
Correct |
138 ms |
25492 KB |
Output is correct |
48 |
Correct |
15 ms |
25172 KB |
Output is correct |
49 |
Correct |
15 ms |
25128 KB |
Output is correct |
50 |
Correct |
14 ms |
25044 KB |
Output is correct |
51 |
Correct |
42 ms |
25968 KB |
Output is correct |
52 |
Correct |
44 ms |
26024 KB |
Output is correct |
53 |
Correct |
20 ms |
25428 KB |
Output is correct |
54 |
Correct |
12 ms |
24504 KB |
Output is correct |
55 |
Correct |
12 ms |
24548 KB |
Output is correct |
56 |
Correct |
17 ms |
25700 KB |
Output is correct |
57 |
Correct |
53 ms |
24660 KB |
Output is correct |
58 |
Correct |
18 ms |
25428 KB |
Output is correct |
59 |
Correct |
20 ms |
25480 KB |
Output is correct |
60 |
Correct |
25 ms |
25476 KB |
Output is correct |
61 |
Correct |
25 ms |
25512 KB |
Output is correct |
62 |
Correct |
39 ms |
25768 KB |
Output is correct |
63 |
Correct |
38 ms |
25816 KB |
Output is correct |
64 |
Correct |
44 ms |
25840 KB |
Output is correct |
65 |
Correct |
211 ms |
26664 KB |
Output is correct |
66 |
Correct |
650 ms |
26532 KB |
Output is correct |
67 |
Correct |
640 ms |
26576 KB |
Output is correct |