#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> doge(n + 1);
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
doge[b[i]].emplace_back(i);
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push({0, {b[0], 0}});
vector<vector<int>> dist(n, vector<int>(200, 1e9));
dist[b[0]][0] = 0;
int limit = (int)sqrt(n);
int res = 1e9;
while (!pq.empty()) {
pair<int, pair<int, int>> cur = pq.top();
pq.pop();
int du = cur.first;
int u = cur.second.first;
if (u == b[1]) {
res = min(res, du);
}
int pu = cur.second.second;
if (pu == 0) {
for (int id : doge[u]) {
if (p[id] <= limit) { // nsqrt(n)
if (dist[u][p[id]] > du) {
dist[u][p[id]] = du;
pq.push({du, {u, p[id]}});
}
} else { // msqrt(n)
int pv = p[id];
for (int j = 1; u - j * pv >= 0; j++) {
int v = u - j * pv;
int dv = du + j;
if (dist[v][0] > dv) {
dist[v][0] = dv;
pq.push({dv, {v, 0}});
}
}
for (int j = 1; u + j * pv < n; j++) {
int v = u + j * pv;
int dv = du + j;
if (dist[v][0] > dv) {
dist[v][0] = dv;
pq.push({dv, {v, 0}});
}
}
}
}
} else {
if (dist[u][0] > du) { // nsqrt(n)
dist[u][0] = du;
pq.push({dist[u][0], {u, 0}});
}
// 2nsqrt(n)
if (u + pu < n && dist[u + pu][pu] > dist[u][pu] + 1) {
int dv = dist[u][pu] + 1;
int v = u + pu;
dist[v][pu] = dist[u][pu] + 1;
pq.push({dv, {v, pu}});
}
if (u - pu >= 0 && dist[u - pu][pu] > dist[u][pu] + 1) {
int dv = dist[u][pu] + 1;
int v = u - pu;
dist[v][pu] = dist[u][pu] + 1;
pq.push({dv, {v, pu}});
}
}
}
cout << (res == 1e9 ? -1 : res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
2 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1748 KB |
Output is correct |
19 |
Correct |
1 ms |
1992 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
1 ms |
584 KB |
Output is correct |
22 |
Correct |
1 ms |
1760 KB |
Output is correct |
23 |
Correct |
2 ms |
1632 KB |
Output is correct |
24 |
Correct |
2 ms |
1992 KB |
Output is correct |
25 |
Correct |
2 ms |
2004 KB |
Output is correct |
26 |
Correct |
2 ms |
2004 KB |
Output is correct |
27 |
Correct |
1 ms |
2004 KB |
Output is correct |
28 |
Correct |
2 ms |
2000 KB |
Output is correct |
29 |
Correct |
7 ms |
2052 KB |
Output is correct |
30 |
Correct |
3 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
2004 KB |
Output is correct |
32 |
Correct |
3 ms |
1988 KB |
Output is correct |
33 |
Correct |
11 ms |
2124 KB |
Output is correct |
34 |
Correct |
12 ms |
2128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
224 KB |
Output is correct |
5 |
Correct |
0 ms |
224 KB |
Output is correct |
6 |
Correct |
1 ms |
224 KB |
Output is correct |
7 |
Correct |
0 ms |
220 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1748 KB |
Output is correct |
19 |
Correct |
1 ms |
1992 KB |
Output is correct |
20 |
Correct |
2 ms |
2004 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
1620 KB |
Output is correct |
24 |
Correct |
3 ms |
1992 KB |
Output is correct |
25 |
Correct |
3 ms |
1992 KB |
Output is correct |
26 |
Correct |
2 ms |
1992 KB |
Output is correct |
27 |
Correct |
2 ms |
2008 KB |
Output is correct |
28 |
Correct |
2 ms |
2008 KB |
Output is correct |
29 |
Correct |
8 ms |
1996 KB |
Output is correct |
30 |
Correct |
3 ms |
2012 KB |
Output is correct |
31 |
Correct |
6 ms |
2068 KB |
Output is correct |
32 |
Correct |
3 ms |
2112 KB |
Output is correct |
33 |
Correct |
11 ms |
2192 KB |
Output is correct |
34 |
Correct |
11 ms |
2136 KB |
Output is correct |
35 |
Correct |
10 ms |
2264 KB |
Output is correct |
36 |
Correct |
4 ms |
1264 KB |
Output is correct |
37 |
Correct |
18 ms |
2516 KB |
Output is correct |
38 |
Correct |
14 ms |
2848 KB |
Output is correct |
39 |
Correct |
15 ms |
2892 KB |
Output is correct |
40 |
Correct |
15 ms |
2772 KB |
Output is correct |
41 |
Correct |
15 ms |
2896 KB |
Output is correct |
42 |
Correct |
5 ms |
2516 KB |
Output is correct |
43 |
Correct |
5 ms |
2504 KB |
Output is correct |
44 |
Correct |
5 ms |
2516 KB |
Output is correct |
45 |
Correct |
15 ms |
2780 KB |
Output is correct |
46 |
Correct |
15 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
216 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
1 ms |
480 KB |
Output is correct |
12 |
Correct |
1 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
472 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1748 KB |
Output is correct |
19 |
Correct |
2 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2008 KB |
Output is correct |
21 |
Correct |
1 ms |
628 KB |
Output is correct |
22 |
Correct |
1 ms |
1748 KB |
Output is correct |
23 |
Correct |
2 ms |
1620 KB |
Output is correct |
24 |
Correct |
2 ms |
2004 KB |
Output is correct |
25 |
Correct |
2 ms |
2092 KB |
Output is correct |
26 |
Correct |
2 ms |
2004 KB |
Output is correct |
27 |
Correct |
2 ms |
2004 KB |
Output is correct |
28 |
Correct |
2 ms |
1988 KB |
Output is correct |
29 |
Correct |
8 ms |
1988 KB |
Output is correct |
30 |
Correct |
4 ms |
2004 KB |
Output is correct |
31 |
Correct |
6 ms |
2004 KB |
Output is correct |
32 |
Correct |
3 ms |
2004 KB |
Output is correct |
33 |
Correct |
13 ms |
2180 KB |
Output is correct |
34 |
Correct |
12 ms |
2184 KB |
Output is correct |
35 |
Correct |
10 ms |
2260 KB |
Output is correct |
36 |
Correct |
3 ms |
1364 KB |
Output is correct |
37 |
Correct |
13 ms |
2540 KB |
Output is correct |
38 |
Correct |
14 ms |
2780 KB |
Output is correct |
39 |
Correct |
14 ms |
2772 KB |
Output is correct |
40 |
Correct |
14 ms |
2852 KB |
Output is correct |
41 |
Correct |
15 ms |
2772 KB |
Output is correct |
42 |
Correct |
5 ms |
2516 KB |
Output is correct |
43 |
Correct |
8 ms |
2516 KB |
Output is correct |
44 |
Correct |
5 ms |
2516 KB |
Output is correct |
45 |
Correct |
15 ms |
2772 KB |
Output is correct |
46 |
Correct |
16 ms |
2772 KB |
Output is correct |
47 |
Correct |
84 ms |
12028 KB |
Output is correct |
48 |
Correct |
14 ms |
20820 KB |
Output is correct |
49 |
Correct |
15 ms |
22604 KB |
Output is correct |
50 |
Correct |
14 ms |
24472 KB |
Output is correct |
51 |
Correct |
55 ms |
27288 KB |
Output is correct |
52 |
Correct |
64 ms |
27304 KB |
Output is correct |
53 |
Correct |
25 ms |
26776 KB |
Output is correct |
54 |
Correct |
13 ms |
25684 KB |
Output is correct |
55 |
Correct |
17 ms |
25604 KB |
Output is correct |
56 |
Correct |
26 ms |
27068 KB |
Output is correct |
57 |
Correct |
83 ms |
25824 KB |
Output is correct |
58 |
Correct |
19 ms |
26068 KB |
Output is correct |
59 |
Correct |
21 ms |
26196 KB |
Output is correct |
60 |
Correct |
25 ms |
26188 KB |
Output is correct |
61 |
Correct |
23 ms |
26292 KB |
Output is correct |
62 |
Correct |
44 ms |
27092 KB |
Output is correct |
63 |
Correct |
59 ms |
27980 KB |
Output is correct |
64 |
Correct |
65 ms |
27972 KB |
Output is correct |
65 |
Correct |
279 ms |
27940 KB |
Output is correct |
66 |
Correct |
811 ms |
29504 KB |
Output is correct |
67 |
Correct |
818 ms |
29436 KB |
Output is correct |