#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
typedef pair<int, int> pi;
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N, M;
cin >> N >> M;
int B[M], P[M];
map<int, vector<int>> dists;
map<int, int> m[N];
for (int i = 0; i < M; ++i) {
cin >> B[i] >> P[i];
dists[B[i]].PB(P[i]);
}
queue<pi> bfs({{B[0], P[0]}});
m[B[0]][P[0]] = 1;
while (!bfs.empty() && bfs.front().F != B[1]) {
auto tmp = bfs.front();
bfs.pop();
if (tmp.F + tmp.S < N &&
m[tmp.F + tmp.S].find(tmp.S) == m[tmp.F + tmp.S].end()) {
m[tmp.F + tmp.S][tmp.S] = m[tmp.F][tmp.S] + 1;
bfs.push({tmp.F + tmp.S, tmp.S});
}
if (tmp.F - tmp.S >= 0 &&
m[tmp.F - tmp.S].find(tmp.S) == m[tmp.F - tmp.S].end()) {
m[tmp.F - tmp.S][tmp.S] = m[tmp.F][tmp.S] + 1;
bfs.push({tmp.F - tmp.S, tmp.S});
}
auto tmp1 = dists.find(tmp.F);
if (tmp1 != dists.end()) {
for (auto i : tmp1->S) {
m[tmp.F][i] = m[tmp.F][tmp.S];
if (tmp.F + i < N && m[tmp.F + i].find(i) == m[tmp.F + i].end()) {
m[tmp.F + i][i] = m[tmp.F][i] + 1;
bfs.push({tmp.F + i, i});
}
if (tmp.F - i >= 0 && m[tmp.F - i].find(i) == m[tmp.F - i].end()) {
m[tmp.F - i][i] = m[tmp.F][i] + 1;
bfs.push({tmp.F - i, i});
}
}
}
}
if (bfs.front().F == B[1])
cout << m[B[1]][bfs.front().S] - 1;
else
cout << -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
6 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
1 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
6 ms |
576 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
588 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 |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
388 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
580 KB |
Output is correct |
28 |
Correct |
9 ms |
1524 KB |
Output is correct |
29 |
Correct |
19 ms |
3276 KB |
Output is correct |
30 |
Correct |
5 ms |
1100 KB |
Output is correct |
31 |
Correct |
11 ms |
1960 KB |
Output is correct |
32 |
Correct |
7 ms |
1484 KB |
Output is correct |
33 |
Correct |
69 ms |
5948 KB |
Output is correct |
34 |
Correct |
34 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
312 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 |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
6 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
588 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 |
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 |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
844 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
8 ms |
1484 KB |
Output is correct |
29 |
Correct |
19 ms |
3176 KB |
Output is correct |
30 |
Correct |
5 ms |
1100 KB |
Output is correct |
31 |
Correct |
9 ms |
1868 KB |
Output is correct |
32 |
Correct |
6 ms |
1484 KB |
Output is correct |
33 |
Correct |
56 ms |
6084 KB |
Output is correct |
34 |
Correct |
27 ms |
3404 KB |
Output is correct |
35 |
Correct |
48 ms |
3428 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
15 ms |
2504 KB |
Output is correct |
38 |
Correct |
27 ms |
3692 KB |
Output is correct |
39 |
Correct |
10 ms |
1348 KB |
Output is correct |
40 |
Correct |
11 ms |
1284 KB |
Output is correct |
41 |
Correct |
12 ms |
1712 KB |
Output is correct |
42 |
Correct |
6 ms |
1100 KB |
Output is correct |
43 |
Correct |
7 ms |
1100 KB |
Output is correct |
44 |
Correct |
7 ms |
1228 KB |
Output is correct |
45 |
Correct |
830 ms |
23492 KB |
Output is correct |
46 |
Correct |
520 ms |
13876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 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 |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
6 ms |
568 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
588 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 |
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 |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
840 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
8 ms |
1484 KB |
Output is correct |
29 |
Correct |
21 ms |
3264 KB |
Output is correct |
30 |
Correct |
4 ms |
1100 KB |
Output is correct |
31 |
Correct |
9 ms |
1848 KB |
Output is correct |
32 |
Correct |
6 ms |
1484 KB |
Output is correct |
33 |
Correct |
54 ms |
5956 KB |
Output is correct |
34 |
Correct |
27 ms |
3480 KB |
Output is correct |
35 |
Correct |
41 ms |
3460 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
15 ms |
2384 KB |
Output is correct |
38 |
Correct |
29 ms |
3744 KB |
Output is correct |
39 |
Correct |
11 ms |
1248 KB |
Output is correct |
40 |
Correct |
11 ms |
1356 KB |
Output is correct |
41 |
Correct |
12 ms |
1740 KB |
Output is correct |
42 |
Correct |
6 ms |
1020 KB |
Output is correct |
43 |
Correct |
6 ms |
1100 KB |
Output is correct |
44 |
Correct |
7 ms |
1220 KB |
Output is correct |
45 |
Correct |
829 ms |
23620 KB |
Output is correct |
46 |
Correct |
542 ms |
13808 KB |
Output is correct |
47 |
Correct |
15 ms |
3148 KB |
Output is correct |
48 |
Correct |
13 ms |
3276 KB |
Output is correct |
49 |
Correct |
12 ms |
3364 KB |
Output is correct |
50 |
Correct |
9 ms |
3148 KB |
Output is correct |
51 |
Correct |
38 ms |
6956 KB |
Output is correct |
52 |
Correct |
45 ms |
7324 KB |
Output is correct |
53 |
Correct |
19 ms |
4684 KB |
Output is correct |
54 |
Correct |
3 ms |
2360 KB |
Output is correct |
55 |
Correct |
4 ms |
2892 KB |
Output is correct |
56 |
Correct |
24 ms |
6860 KB |
Output is correct |
57 |
Correct |
3 ms |
1996 KB |
Output is correct |
58 |
Correct |
16 ms |
5324 KB |
Output is correct |
59 |
Correct |
15 ms |
4988 KB |
Output is correct |
60 |
Correct |
16 ms |
4940 KB |
Output is correct |
61 |
Correct |
15 ms |
4836 KB |
Output is correct |
62 |
Correct |
452 ms |
26596 KB |
Output is correct |
63 |
Execution timed out |
1093 ms |
57912 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |