#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<pair<int, int>> q;
vector<unordered_map<int, int>> dp(n);
vector<bool> mark(n);
for (int p : g[source]) {
q.emplace(source, p);
dp[source][p] = 0;
mark[0] = true;
}
while (!q.empty()) {
int u, p;
tie(u, p) = q.front();
q.pop();
if (0 <= u - p) {
if (dp[u - p].find(p) == dp[u - p].end()) {
dp[u - p][p] = dp[u][p] + 1;
q.emplace(u - p, p);
}
}
if (u + p < n) {
if (dp[u + p].find(p) == dp[u + p].end()) {
dp[u + p][p] = dp[u][p] + 1;
q.emplace(u + p, p);
}
}
if (!mark[u]) {
for (int pp : g[u]) {
if (dp[u].find(pp) == dp[u].end()) {
dp[u][pp] = dp[u][p];
q.emplace(u, pp);
}
}
mark[u] = true;
}
}
int best = INF;
for (auto it : dp[sink]) {
minSelf(best, it.second);
}
if (best == INF) {
best = -1;
}
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 |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 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 |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
588 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 |
1 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 |
1 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 |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
704 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
972 KB |
Output is correct |
25 |
Correct |
3 ms |
844 KB |
Output is correct |
26 |
Correct |
3 ms |
844 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
4 ms |
1356 KB |
Output is correct |
29 |
Correct |
14 ms |
3284 KB |
Output is correct |
30 |
Correct |
3 ms |
1100 KB |
Output is correct |
31 |
Correct |
6 ms |
1996 KB |
Output is correct |
32 |
Correct |
4 ms |
1320 KB |
Output is correct |
33 |
Correct |
22 ms |
5956 KB |
Output is correct |
34 |
Correct |
22 ms |
5956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
972 KB |
Output is correct |
25 |
Correct |
3 ms |
844 KB |
Output is correct |
26 |
Correct |
3 ms |
844 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
4 ms |
1356 KB |
Output is correct |
29 |
Correct |
10 ms |
3276 KB |
Output is correct |
30 |
Correct |
3 ms |
1100 KB |
Output is correct |
31 |
Correct |
6 ms |
1996 KB |
Output is correct |
32 |
Correct |
3 ms |
1356 KB |
Output is correct |
33 |
Correct |
21 ms |
6028 KB |
Output is correct |
34 |
Correct |
22 ms |
5972 KB |
Output is correct |
35 |
Correct |
25 ms |
4812 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
49 ms |
8044 KB |
Output is correct |
38 |
Correct |
43 ms |
7628 KB |
Output is correct |
39 |
Correct |
44 ms |
7652 KB |
Output is correct |
40 |
Correct |
45 ms |
7688 KB |
Output is correct |
41 |
Correct |
48 ms |
7724 KB |
Output is correct |
42 |
Correct |
6 ms |
1188 KB |
Output is correct |
43 |
Correct |
7 ms |
1228 KB |
Output is correct |
44 |
Correct |
5 ms |
1100 KB |
Output is correct |
45 |
Correct |
101 ms |
19876 KB |
Output is correct |
46 |
Correct |
97 ms |
19908 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 |
320 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 |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
984 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
2 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
3 ms |
992 KB |
Output is correct |
25 |
Correct |
2 ms |
844 KB |
Output is correct |
26 |
Correct |
2 ms |
844 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
4 ms |
1356 KB |
Output is correct |
29 |
Correct |
13 ms |
3276 KB |
Output is correct |
30 |
Correct |
3 ms |
1160 KB |
Output is correct |
31 |
Correct |
8 ms |
1996 KB |
Output is correct |
32 |
Correct |
4 ms |
1356 KB |
Output is correct |
33 |
Correct |
30 ms |
6064 KB |
Output is correct |
34 |
Correct |
22 ms |
6028 KB |
Output is correct |
35 |
Correct |
24 ms |
4672 KB |
Output is correct |
36 |
Correct |
4 ms |
1100 KB |
Output is correct |
37 |
Correct |
50 ms |
7876 KB |
Output is correct |
38 |
Correct |
43 ms |
7412 KB |
Output is correct |
39 |
Correct |
48 ms |
7412 KB |
Output is correct |
40 |
Correct |
45 ms |
7452 KB |
Output is correct |
41 |
Correct |
48 ms |
7464 KB |
Output is correct |
42 |
Correct |
7 ms |
972 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
864 KB |
Output is correct |
45 |
Correct |
105 ms |
19716 KB |
Output is correct |
46 |
Correct |
105 ms |
19816 KB |
Output is correct |
47 |
Correct |
550 ms |
34276 KB |
Output is correct |
48 |
Correct |
6 ms |
2636 KB |
Output is correct |
49 |
Correct |
8 ms |
2748 KB |
Output is correct |
50 |
Correct |
4 ms |
2872 KB |
Output is correct |
51 |
Correct |
115 ms |
16896 KB |
Output is correct |
52 |
Correct |
134 ms |
18116 KB |
Output is correct |
53 |
Correct |
21 ms |
7884 KB |
Output is correct |
54 |
Correct |
8 ms |
4684 KB |
Output is correct |
55 |
Correct |
8 ms |
4792 KB |
Output is correct |
56 |
Correct |
25 ms |
7908 KB |
Output is correct |
57 |
Correct |
171 ms |
23684 KB |
Output is correct |
58 |
Correct |
26 ms |
8260 KB |
Output is correct |
59 |
Correct |
28 ms |
9196 KB |
Output is correct |
60 |
Correct |
44 ms |
10008 KB |
Output is correct |
61 |
Correct |
38 ms |
9516 KB |
Output is correct |
62 |
Correct |
179 ms |
23076 KB |
Output is correct |
63 |
Execution timed out |
1092 ms |
96792 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |