#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];
vector<int> dists[N];
map<int, int> m[N];
vector<bool> vis(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]}, {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 && tmp.F + tmp.S >= 0 &&
m[tmp.F + tmp.S].find(abs(tmp.S)) == m[tmp.F + tmp.S].end()) {
m[tmp.F + tmp.S][abs(tmp.S)] = m[tmp.F][abs(tmp.S)] + 1;
bfs.push({tmp.F + tmp.S, tmp.S});
}
if (!vis[tmp.F])
for (auto i : dists[tmp.F]) {
m[tmp.F][i] = m[tmp.F][abs(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});
}
}
vis[tmp.F] = 1;
}
cout << (bfs.empty() ? -1 : m[B[1]][abs(bfs.front().S)] - 1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
2 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 |
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 |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
16 ms |
3200 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
7 ms |
1952 KB |
Output is correct |
32 |
Correct |
6 ms |
1484 KB |
Output is correct |
33 |
Correct |
39 ms |
6040 KB |
Output is correct |
34 |
Correct |
18 ms |
3404 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 |
268 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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 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 |
2 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 |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
15 ms |
3244 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
7 ms |
1996 KB |
Output is correct |
32 |
Correct |
7 ms |
1484 KB |
Output is correct |
33 |
Correct |
38 ms |
6080 KB |
Output is correct |
34 |
Correct |
18 ms |
3564 KB |
Output is correct |
35 |
Correct |
17 ms |
3148 KB |
Output is correct |
36 |
Correct |
2 ms |
464 KB |
Output is correct |
37 |
Correct |
11 ms |
2152 KB |
Output is correct |
38 |
Correct |
19 ms |
3276 KB |
Output is correct |
39 |
Correct |
9 ms |
844 KB |
Output is correct |
40 |
Correct |
7 ms |
972 KB |
Output is correct |
41 |
Correct |
9 ms |
1356 KB |
Output is correct |
42 |
Correct |
5 ms |
844 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
844 KB |
Output is correct |
45 |
Correct |
301 ms |
23328 KB |
Output is correct |
46 |
Correct |
124 ms |
13808 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 |
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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 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 |
2 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 |
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 |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
5 ms |
1356 KB |
Output is correct |
29 |
Correct |
15 ms |
3252 KB |
Output is correct |
30 |
Correct |
4 ms |
1228 KB |
Output is correct |
31 |
Correct |
7 ms |
1996 KB |
Output is correct |
32 |
Correct |
5 ms |
1484 KB |
Output is correct |
33 |
Correct |
41 ms |
5988 KB |
Output is correct |
34 |
Correct |
19 ms |
3404 KB |
Output is correct |
35 |
Correct |
17 ms |
3148 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
11 ms |
2124 KB |
Output is correct |
38 |
Correct |
18 ms |
3356 KB |
Output is correct |
39 |
Correct |
7 ms |
916 KB |
Output is correct |
40 |
Correct |
7 ms |
972 KB |
Output is correct |
41 |
Correct |
8 ms |
1400 KB |
Output is correct |
42 |
Correct |
5 ms |
844 KB |
Output is correct |
43 |
Correct |
6 ms |
972 KB |
Output is correct |
44 |
Correct |
6 ms |
844 KB |
Output is correct |
45 |
Correct |
313 ms |
23576 KB |
Output is correct |
46 |
Correct |
119 ms |
13644 KB |
Output is correct |
47 |
Correct |
11 ms |
2380 KB |
Output is correct |
48 |
Correct |
7 ms |
2508 KB |
Output is correct |
49 |
Correct |
7 ms |
2636 KB |
Output is correct |
50 |
Correct |
5 ms |
2636 KB |
Output is correct |
51 |
Correct |
20 ms |
5836 KB |
Output is correct |
52 |
Correct |
22 ms |
6224 KB |
Output is correct |
53 |
Correct |
10 ms |
3532 KB |
Output is correct |
54 |
Correct |
3 ms |
3020 KB |
Output is correct |
55 |
Correct |
4 ms |
3532 KB |
Output is correct |
56 |
Correct |
14 ms |
4940 KB |
Output is correct |
57 |
Correct |
2 ms |
2512 KB |
Output is correct |
58 |
Correct |
15 ms |
5820 KB |
Output is correct |
59 |
Correct |
14 ms |
5444 KB |
Output is correct |
60 |
Correct |
14 ms |
5496 KB |
Output is correct |
61 |
Correct |
14 ms |
5196 KB |
Output is correct |
62 |
Correct |
210 ms |
24552 KB |
Output is correct |
63 |
Execution timed out |
1101 ms |
73828 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |