#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
void minSelf(int &x, int y) {
if (y < x) {
x = y;
}
}
void testCase() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
int source = 0, sink = 0;
for (int i = 0; i < m; ++i) {
int b, p;
cin >> b >> p;
if (0 <= b - p || b + p < n) {
g[b].emplace_back(p);
}
if (i == 0) {
source = b;
}
if (i == 1) {
sink = b;
}
}
if (source == sink) {
cout << "0\n";
return;
}
for (int v = 0; v < n; ++v) {
sort(g[v].begin(), g[v].end());
g[v].erase(unique(g[v].begin(), g[v].end()), g[v].end());
}
queue<tuple<int, int, int>> q;
unordered_set<int> vis;
vector<bool> mark(n);
int best = INF;
auto convert = [&](int x, int y) -> int {
return x * n + y;
};
for (int p : g[source]) {
q.emplace(0, source, p);
vis.emplace(convert(source, p));
mark[0] = true;
}
int checkpoint = clock();
while (!q.empty() && clock() - checkpoint < 0.9 * CLOCKS_PER_SEC) {
int cost, u, p;
tie(cost, u, p) = q.front();
q.pop();
if (u == sink) {
minSelf(best, cost);
}
for (int sgn : {-1, 1}) {
int v = u + sgn * p;
if (0 <= v && v < n) {
int state = convert(v, p);
if (!vis.count(state)) {
vis.emplace(state);
q.emplace(cost + 1, v, p);
}
}
}
if (!mark[u]) {
for (int pp : g[u]) {
int state = convert(u, pp);
if (!vis.count(state)) {
vis.emplace(state);
q.emplace(cost, u, pp);
}
}
mark[u] = true;
}
}
if (best == INF) {
cout << "-1\n";
} else {
cout << best << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
7 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
844 KB |
Output is correct |
25 |
Correct |
3 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
3 ms |
588 KB |
Output is correct |
28 |
Correct |
9 ms |
1100 KB |
Output is correct |
29 |
Correct |
26 ms |
2848 KB |
Output is correct |
30 |
Correct |
8 ms |
964 KB |
Output is correct |
31 |
Correct |
14 ms |
1704 KB |
Output is correct |
32 |
Correct |
10 ms |
1484 KB |
Output is correct |
33 |
Correct |
52 ms |
5428 KB |
Output is correct |
34 |
Correct |
51 ms |
5432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
480 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
8 ms |
908 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
3 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
844 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
4 ms |
588 KB |
Output is correct |
28 |
Correct |
9 ms |
1164 KB |
Output is correct |
29 |
Correct |
26 ms |
2908 KB |
Output is correct |
30 |
Correct |
9 ms |
1072 KB |
Output is correct |
31 |
Correct |
14 ms |
1704 KB |
Output is correct |
32 |
Correct |
11 ms |
1484 KB |
Output is correct |
33 |
Correct |
50 ms |
5472 KB |
Output is correct |
34 |
Correct |
51 ms |
5432 KB |
Output is correct |
35 |
Correct |
55 ms |
5152 KB |
Output is correct |
36 |
Correct |
8 ms |
972 KB |
Output is correct |
37 |
Correct |
88 ms |
7168 KB |
Output is correct |
38 |
Correct |
80 ms |
6648 KB |
Output is correct |
39 |
Correct |
79 ms |
6628 KB |
Output is correct |
40 |
Correct |
78 ms |
6584 KB |
Output is correct |
41 |
Correct |
84 ms |
6556 KB |
Output is correct |
42 |
Correct |
7 ms |
716 KB |
Output is correct |
43 |
Correct |
7 ms |
780 KB |
Output is correct |
44 |
Correct |
5 ms |
588 KB |
Output is correct |
45 |
Correct |
230 ms |
21168 KB |
Output is correct |
46 |
Correct |
233 ms |
21136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
3 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
7 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
6 ms |
844 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
4 ms |
588 KB |
Output is correct |
28 |
Correct |
9 ms |
1100 KB |
Output is correct |
29 |
Correct |
26 ms |
2908 KB |
Output is correct |
30 |
Correct |
8 ms |
972 KB |
Output is correct |
31 |
Correct |
14 ms |
1688 KB |
Output is correct |
32 |
Correct |
10 ms |
1484 KB |
Output is correct |
33 |
Correct |
51 ms |
5428 KB |
Output is correct |
34 |
Correct |
51 ms |
5404 KB |
Output is correct |
35 |
Correct |
47 ms |
5140 KB |
Output is correct |
36 |
Correct |
9 ms |
956 KB |
Output is correct |
37 |
Correct |
103 ms |
7220 KB |
Output is correct |
38 |
Correct |
78 ms |
6624 KB |
Output is correct |
39 |
Correct |
80 ms |
6580 KB |
Output is correct |
40 |
Correct |
86 ms |
6612 KB |
Output is correct |
41 |
Correct |
83 ms |
6580 KB |
Output is correct |
42 |
Correct |
7 ms |
716 KB |
Output is correct |
43 |
Correct |
7 ms |
716 KB |
Output is correct |
44 |
Correct |
5 ms |
588 KB |
Output is correct |
45 |
Correct |
223 ms |
21188 KB |
Output is correct |
46 |
Correct |
223 ms |
21184 KB |
Output is correct |
47 |
Correct |
561 ms |
40400 KB |
Output is correct |
48 |
Correct |
5 ms |
1228 KB |
Output is correct |
49 |
Correct |
5 ms |
1184 KB |
Output is correct |
50 |
Correct |
4 ms |
1228 KB |
Output is correct |
51 |
Correct |
178 ms |
13828 KB |
Output is correct |
52 |
Correct |
198 ms |
14744 KB |
Output is correct |
53 |
Correct |
31 ms |
3540 KB |
Output is correct |
54 |
Correct |
7 ms |
1612 KB |
Output is correct |
55 |
Correct |
7 ms |
1612 KB |
Output is correct |
56 |
Correct |
20 ms |
3232 KB |
Output is correct |
57 |
Correct |
244 ms |
20428 KB |
Output is correct |
58 |
Correct |
31 ms |
3804 KB |
Output is correct |
59 |
Correct |
48 ms |
5776 KB |
Output is correct |
60 |
Correct |
65 ms |
6332 KB |
Output is correct |
61 |
Correct |
57 ms |
5828 KB |
Output is correct |
62 |
Correct |
271 ms |
21000 KB |
Output is correct |
63 |
Incorrect |
990 ms |
54708 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |