#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 30000;
const int C = 100;
vector<pair<int, int>> G[MAX_N * C];
int B[MAX_N], P[MAX_N];
int D[MAX_N * C];
void connect(int u, int v, int w, bool directed = false) {
G[u].push_back({v, w});
if (!directed) {
G[v].push_back({u, w});
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
if (P[i] >= C) {
for (int j = B[i] % P[i]; j < N; j += P[i]) {
connect(B[i], j, abs(j - B[i]) / P[i], true);
}
} else {
connect(B[i], B[i] + P[i] * N, 0, true);
}
}
for (int i = 0; i < N; i++) {
for (int power = 1; power < C; power++) {
if (i - power >= 0) {
connect(i + power * N, i + power * (N - 1), 1, true);
connect(i + power * N, i - power, 1, true);
}
if (i + power < N) {
connect(i + power * N, i + power * (N + 1), 1, true);
connect(i + power * N, i + power, 1, true);
}
}
}
fill(D, D + MAX_N * C, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
Q.push({D[B[0]] = 0, B[0]});
while (!Q.empty()) {
auto [d, u] = Q.top();
Q.pop();
if (d > D[u]) {
continue;
}
for (auto [v, w] : G[u]) {
if (D[u] + w < D[v]) {
Q.push({D[v] = D[u] + w, v});
}
}
}
cout << (D[B[1]] == INT_MAX ? -1 : D[B[1]]) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
82380 KB |
Output is correct |
2 |
Correct |
32 ms |
82380 KB |
Output is correct |
3 |
Correct |
33 ms |
82392 KB |
Output is correct |
4 |
Correct |
32 ms |
82516 KB |
Output is correct |
5 |
Correct |
32 ms |
82544 KB |
Output is correct |
6 |
Correct |
33 ms |
82516 KB |
Output is correct |
7 |
Correct |
32 ms |
82516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
82436 KB |
Output is correct |
2 |
Correct |
34 ms |
82516 KB |
Output is correct |
3 |
Correct |
33 ms |
82516 KB |
Output is correct |
4 |
Correct |
32 ms |
82516 KB |
Output is correct |
5 |
Correct |
32 ms |
82444 KB |
Output is correct |
6 |
Correct |
32 ms |
82528 KB |
Output is correct |
7 |
Correct |
33 ms |
82516 KB |
Output is correct |
8 |
Correct |
33 ms |
82444 KB |
Output is correct |
9 |
Correct |
33 ms |
82504 KB |
Output is correct |
10 |
Correct |
34 ms |
82772 KB |
Output is correct |
11 |
Correct |
34 ms |
82772 KB |
Output is correct |
12 |
Correct |
37 ms |
82728 KB |
Output is correct |
13 |
Correct |
37 ms |
82764 KB |
Output is correct |
14 |
Correct |
34 ms |
82808 KB |
Output is correct |
15 |
Correct |
36 ms |
82772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
82500 KB |
Output is correct |
2 |
Correct |
32 ms |
82380 KB |
Output is correct |
3 |
Correct |
32 ms |
82380 KB |
Output is correct |
4 |
Correct |
32 ms |
82516 KB |
Output is correct |
5 |
Correct |
33 ms |
82516 KB |
Output is correct |
6 |
Correct |
38 ms |
82396 KB |
Output is correct |
7 |
Correct |
32 ms |
82516 KB |
Output is correct |
8 |
Correct |
33 ms |
82516 KB |
Output is correct |
9 |
Correct |
33 ms |
82516 KB |
Output is correct |
10 |
Correct |
34 ms |
82772 KB |
Output is correct |
11 |
Correct |
34 ms |
82764 KB |
Output is correct |
12 |
Correct |
36 ms |
82784 KB |
Output is correct |
13 |
Correct |
33 ms |
82772 KB |
Output is correct |
14 |
Correct |
35 ms |
82772 KB |
Output is correct |
15 |
Correct |
34 ms |
82772 KB |
Output is correct |
16 |
Correct |
35 ms |
83272 KB |
Output is correct |
17 |
Correct |
43 ms |
85836 KB |
Output is correct |
18 |
Correct |
55 ms |
90444 KB |
Output is correct |
19 |
Correct |
56 ms |
91596 KB |
Output is correct |
20 |
Correct |
55 ms |
91720 KB |
Output is correct |
21 |
Correct |
37 ms |
84180 KB |
Output is correct |
22 |
Correct |
58 ms |
90628 KB |
Output is correct |
23 |
Correct |
52 ms |
89844 KB |
Output is correct |
24 |
Correct |
55 ms |
91264 KB |
Output is correct |
25 |
Correct |
56 ms |
91596 KB |
Output is correct |
26 |
Correct |
56 ms |
91604 KB |
Output is correct |
27 |
Correct |
56 ms |
91652 KB |
Output is correct |
28 |
Correct |
59 ms |
91772 KB |
Output is correct |
29 |
Correct |
63 ms |
91724 KB |
Output is correct |
30 |
Correct |
57 ms |
91596 KB |
Output is correct |
31 |
Correct |
59 ms |
91668 KB |
Output is correct |
32 |
Correct |
58 ms |
91596 KB |
Output is correct |
33 |
Correct |
69 ms |
91624 KB |
Output is correct |
34 |
Correct |
68 ms |
91740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
82444 KB |
Output is correct |
2 |
Correct |
33 ms |
82516 KB |
Output is correct |
3 |
Correct |
33 ms |
82380 KB |
Output is correct |
4 |
Correct |
33 ms |
82404 KB |
Output is correct |
5 |
Correct |
33 ms |
82516 KB |
Output is correct |
6 |
Correct |
33 ms |
82404 KB |
Output is correct |
7 |
Correct |
33 ms |
82380 KB |
Output is correct |
8 |
Correct |
33 ms |
82544 KB |
Output is correct |
9 |
Correct |
33 ms |
82516 KB |
Output is correct |
10 |
Correct |
35 ms |
82808 KB |
Output is correct |
11 |
Correct |
39 ms |
82772 KB |
Output is correct |
12 |
Correct |
35 ms |
82764 KB |
Output is correct |
13 |
Correct |
34 ms |
82832 KB |
Output is correct |
14 |
Correct |
35 ms |
82864 KB |
Output is correct |
15 |
Correct |
35 ms |
82768 KB |
Output is correct |
16 |
Correct |
36 ms |
83284 KB |
Output is correct |
17 |
Correct |
44 ms |
85844 KB |
Output is correct |
18 |
Correct |
53 ms |
90524 KB |
Output is correct |
19 |
Correct |
64 ms |
91676 KB |
Output is correct |
20 |
Correct |
56 ms |
91728 KB |
Output is correct |
21 |
Correct |
40 ms |
84300 KB |
Output is correct |
22 |
Correct |
54 ms |
90572 KB |
Output is correct |
23 |
Correct |
53 ms |
89804 KB |
Output is correct |
24 |
Correct |
57 ms |
91292 KB |
Output is correct |
25 |
Correct |
57 ms |
91596 KB |
Output is correct |
26 |
Correct |
58 ms |
91596 KB |
Output is correct |
27 |
Correct |
57 ms |
91596 KB |
Output is correct |
28 |
Correct |
58 ms |
91724 KB |
Output is correct |
29 |
Correct |
63 ms |
91596 KB |
Output is correct |
30 |
Correct |
58 ms |
91596 KB |
Output is correct |
31 |
Correct |
60 ms |
91596 KB |
Output is correct |
32 |
Correct |
62 ms |
91676 KB |
Output is correct |
33 |
Correct |
71 ms |
91740 KB |
Output is correct |
34 |
Correct |
70 ms |
91724 KB |
Output is correct |
35 |
Correct |
65 ms |
89960 KB |
Output is correct |
36 |
Correct |
59 ms |
87568 KB |
Output is correct |
37 |
Correct |
77 ms |
92620 KB |
Output is correct |
38 |
Correct |
81 ms |
92960 KB |
Output is correct |
39 |
Correct |
77 ms |
93008 KB |
Output is correct |
40 |
Correct |
81 ms |
93028 KB |
Output is correct |
41 |
Correct |
79 ms |
93004 KB |
Output is correct |
42 |
Correct |
65 ms |
92192 KB |
Output is correct |
43 |
Correct |
60 ms |
92112 KB |
Output is correct |
44 |
Correct |
58 ms |
92152 KB |
Output is correct |
45 |
Correct |
85 ms |
94868 KB |
Output is correct |
46 |
Correct |
90 ms |
94768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
82380 KB |
Output is correct |
2 |
Correct |
34 ms |
82516 KB |
Output is correct |
3 |
Correct |
36 ms |
82504 KB |
Output is correct |
4 |
Correct |
33 ms |
82516 KB |
Output is correct |
5 |
Correct |
38 ms |
82516 KB |
Output is correct |
6 |
Correct |
34 ms |
82484 KB |
Output is correct |
7 |
Correct |
43 ms |
82436 KB |
Output is correct |
8 |
Correct |
33 ms |
82440 KB |
Output is correct |
9 |
Correct |
33 ms |
82540 KB |
Output is correct |
10 |
Correct |
35 ms |
82772 KB |
Output is correct |
11 |
Correct |
36 ms |
82760 KB |
Output is correct |
12 |
Correct |
35 ms |
82772 KB |
Output is correct |
13 |
Correct |
35 ms |
82712 KB |
Output is correct |
14 |
Correct |
34 ms |
82748 KB |
Output is correct |
15 |
Correct |
34 ms |
82772 KB |
Output is correct |
16 |
Correct |
35 ms |
83268 KB |
Output is correct |
17 |
Correct |
43 ms |
85784 KB |
Output is correct |
18 |
Correct |
53 ms |
90488 KB |
Output is correct |
19 |
Correct |
54 ms |
91676 KB |
Output is correct |
20 |
Correct |
55 ms |
91724 KB |
Output is correct |
21 |
Correct |
40 ms |
84180 KB |
Output is correct |
22 |
Correct |
54 ms |
90580 KB |
Output is correct |
23 |
Correct |
52 ms |
89836 KB |
Output is correct |
24 |
Correct |
55 ms |
91220 KB |
Output is correct |
25 |
Correct |
57 ms |
91656 KB |
Output is correct |
26 |
Correct |
56 ms |
91596 KB |
Output is correct |
27 |
Correct |
57 ms |
91592 KB |
Output is correct |
28 |
Correct |
61 ms |
91724 KB |
Output is correct |
29 |
Correct |
70 ms |
91656 KB |
Output is correct |
30 |
Correct |
59 ms |
91596 KB |
Output is correct |
31 |
Correct |
60 ms |
91668 KB |
Output is correct |
32 |
Correct |
69 ms |
91596 KB |
Output is correct |
33 |
Correct |
74 ms |
91736 KB |
Output is correct |
34 |
Correct |
69 ms |
91748 KB |
Output is correct |
35 |
Correct |
64 ms |
89964 KB |
Output is correct |
36 |
Correct |
49 ms |
87500 KB |
Output is correct |
37 |
Correct |
86 ms |
92716 KB |
Output is correct |
38 |
Correct |
78 ms |
93004 KB |
Output is correct |
39 |
Correct |
79 ms |
93028 KB |
Output is correct |
40 |
Correct |
94 ms |
92928 KB |
Output is correct |
41 |
Correct |
78 ms |
92932 KB |
Output is correct |
42 |
Correct |
62 ms |
92116 KB |
Output is correct |
43 |
Correct |
59 ms |
92100 KB |
Output is correct |
44 |
Correct |
62 ms |
92120 KB |
Output is correct |
45 |
Correct |
87 ms |
94816 KB |
Output is correct |
46 |
Correct |
85 ms |
94868 KB |
Output is correct |
47 |
Correct |
318 ms |
144948 KB |
Output is correct |
48 |
Correct |
303 ms |
192260 KB |
Output is correct |
49 |
Correct |
384 ms |
201960 KB |
Output is correct |
50 |
Correct |
395 ms |
213608 KB |
Output is correct |
51 |
Correct |
448 ms |
224972 KB |
Output is correct |
52 |
Correct |
442 ms |
224972 KB |
Output is correct |
53 |
Correct |
397 ms |
222968 KB |
Output is correct |
54 |
Correct |
390 ms |
221808 KB |
Output is correct |
55 |
Correct |
384 ms |
221760 KB |
Output is correct |
56 |
Correct |
389 ms |
223144 KB |
Output is correct |
57 |
Correct |
507 ms |
221876 KB |
Output is correct |
58 |
Correct |
402 ms |
222372 KB |
Output is correct |
59 |
Correct |
397 ms |
222456 KB |
Output is correct |
60 |
Correct |
480 ms |
222992 KB |
Output is correct |
61 |
Correct |
470 ms |
222760 KB |
Output is correct |
62 |
Correct |
435 ms |
225664 KB |
Output is correct |
63 |
Correct |
550 ms |
245020 KB |
Output is correct |
64 |
Correct |
516 ms |
250600 KB |
Output is correct |
65 |
Correct |
537 ms |
259716 KB |
Output is correct |
66 |
Correct |
960 ms |
261712 KB |
Output is correct |
67 |
Correct |
975 ms |
262120 KB |
Output is correct |