#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<map<int, int>> dp(n);
for (int p : g[source]) {
q.emplace(source, p);
dp[source][p] = 0;
}
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);
}
}
for (int pp : g[u]) {
if (dp[u].find(pp) == dp[u].end()) {
dp[u][pp] = dp[u][p];
q.emplace(u, pp);
}
}
}
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 |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
292 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
216 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 |
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 |
320 KB |
Output is correct |
14 |
Correct |
3 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
568 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 |
308 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 |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
332 KB |
Output is correct |
17 |
Correct |
5 ms |
1068 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 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 |
964 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
4 ms |
1472 KB |
Output is correct |
29 |
Correct |
18 ms |
3324 KB |
Output is correct |
30 |
Correct |
3 ms |
1212 KB |
Output is correct |
31 |
Correct |
8 ms |
2024 KB |
Output is correct |
32 |
Correct |
4 ms |
1484 KB |
Output is correct |
33 |
Correct |
47 ms |
5992 KB |
Output is correct |
34 |
Correct |
36 ms |
6068 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 |
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 |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
456 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 |
332 KB |
Output is correct |
17 |
Correct |
5 ms |
1004 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
576 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
716 KB |
Output is correct |
24 |
Correct |
4 ms |
960 KB |
Output is correct |
25 |
Correct |
4 ms |
704 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
704 KB |
Output is correct |
28 |
Correct |
4 ms |
1476 KB |
Output is correct |
29 |
Correct |
14 ms |
3260 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
6 ms |
1996 KB |
Output is correct |
32 |
Correct |
4 ms |
1484 KB |
Output is correct |
33 |
Correct |
34 ms |
6084 KB |
Output is correct |
34 |
Correct |
34 ms |
6020 KB |
Output is correct |
35 |
Correct |
66 ms |
4908 KB |
Output is correct |
36 |
Correct |
5 ms |
1100 KB |
Output is correct |
37 |
Correct |
155 ms |
8764 KB |
Output is correct |
38 |
Correct |
129 ms |
8008 KB |
Output is correct |
39 |
Correct |
140 ms |
8008 KB |
Output is correct |
40 |
Correct |
132 ms |
7940 KB |
Output is correct |
41 |
Correct |
133 ms |
7936 KB |
Output is correct |
42 |
Correct |
6 ms |
1072 KB |
Output is correct |
43 |
Correct |
5 ms |
1100 KB |
Output is correct |
44 |
Correct |
5 ms |
824 KB |
Output is correct |
45 |
Correct |
341 ms |
23352 KB |
Output is correct |
46 |
Correct |
326 ms |
23364 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 |
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 |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
452 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 |
332 KB |
Output is correct |
17 |
Correct |
5 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 |
1 ms |
588 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 |
700 KB |
Output is correct |
24 |
Correct |
4 ms |
972 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
712 KB |
Output is correct |
28 |
Correct |
6 ms |
1484 KB |
Output is correct |
29 |
Correct |
13 ms |
3276 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
6 ms |
1996 KB |
Output is correct |
32 |
Correct |
4 ms |
1484 KB |
Output is correct |
33 |
Correct |
33 ms |
6084 KB |
Output is correct |
34 |
Correct |
36 ms |
6092 KB |
Output is correct |
35 |
Correct |
63 ms |
4936 KB |
Output is correct |
36 |
Correct |
5 ms |
1076 KB |
Output is correct |
37 |
Correct |
174 ms |
8764 KB |
Output is correct |
38 |
Correct |
133 ms |
8012 KB |
Output is correct |
39 |
Correct |
136 ms |
8024 KB |
Output is correct |
40 |
Correct |
132 ms |
8012 KB |
Output is correct |
41 |
Correct |
133 ms |
8104 KB |
Output is correct |
42 |
Correct |
5 ms |
1072 KB |
Output is correct |
43 |
Correct |
5 ms |
1100 KB |
Output is correct |
44 |
Correct |
5 ms |
828 KB |
Output is correct |
45 |
Correct |
385 ms |
23348 KB |
Output is correct |
46 |
Correct |
361 ms |
23344 KB |
Output is correct |
47 |
Correct |
831 ms |
36208 KB |
Output is correct |
48 |
Correct |
8 ms |
2500 KB |
Output is correct |
49 |
Correct |
6 ms |
2620 KB |
Output is correct |
50 |
Correct |
5 ms |
2632 KB |
Output is correct |
51 |
Correct |
158 ms |
17720 KB |
Output is correct |
52 |
Correct |
162 ms |
19012 KB |
Output is correct |
53 |
Correct |
13 ms |
5872 KB |
Output is correct |
54 |
Correct |
3 ms |
3020 KB |
Output is correct |
55 |
Correct |
3 ms |
3532 KB |
Output is correct |
56 |
Correct |
14 ms |
4936 KB |
Output is correct |
57 |
Correct |
220 ms |
22912 KB |
Output is correct |
58 |
Correct |
14 ms |
5708 KB |
Output is correct |
59 |
Correct |
16 ms |
7280 KB |
Output is correct |
60 |
Correct |
25 ms |
8516 KB |
Output is correct |
61 |
Correct |
67 ms |
7836 KB |
Output is correct |
62 |
Correct |
190 ms |
25620 KB |
Output is correct |
63 |
Execution timed out |
1087 ms |
76464 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |