#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 30004;
int b[N];
int seen[N];
vector<int> in[N];
bitset<N> vis[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int p;
cin >> b[i] >> p;
in[b[i]].push_back(p);
}
fill(seen, seen + N, -1);
priority_queue<array<int, 3>> pq;
for (int p : in[b[0]]) {
pq.push({0, p, b[0]});
vis[p][b[0]] = 1;
}
seen[b[0]] = 0;
while (!pq.empty()) {
auto [c, p, v] = pq.top();
c = -c;
if (v == b[1]) {
cout << c << '\n';
return 0;
}
pq.pop();
int forward = v + p;
if (forward < n && !vis[p][forward]) {
if (seen[forward] == -1) {
seen[forward] = c + 1;
for (int np : in[forward]) {
vis[np][forward] = 1;
pq.push({-(c + 1), np, forward});
}
}
vis[p][forward] = 1;
pq.push({-(c + 1), p, forward});
}
int backward = v - p;
if (backward >= 0 && !vis[p][backward]) {
if (seen[backward] == -1) {
seen[backward] = c + 1;
for (int np : in[backward]) {
vis[np][backward] = 1;
pq.push({-(c + 1), np, backward});
}
}
vis[p][backward] = 1;
pq.push({-(c + 1), p, backward});
}
}
cout << seen[b[1]] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4696 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
3672 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
6 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2612 KB |
Output is correct |
32 |
Correct |
4 ms |
4444 KB |
Output is correct |
33 |
Correct |
16 ms |
4700 KB |
Output is correct |
34 |
Correct |
9 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4696 KB |
Output is correct |
16 |
Correct |
1 ms |
4696 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8676 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
3676 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
6 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2392 KB |
Output is correct |
32 |
Correct |
3 ms |
4440 KB |
Output is correct |
33 |
Correct |
13 ms |
4812 KB |
Output is correct |
34 |
Correct |
8 ms |
4700 KB |
Output is correct |
35 |
Correct |
12 ms |
11736 KB |
Output is correct |
36 |
Correct |
3 ms |
9308 KB |
Output is correct |
37 |
Correct |
6 ms |
7452 KB |
Output is correct |
38 |
Correct |
7 ms |
11672 KB |
Output is correct |
39 |
Correct |
5 ms |
10848 KB |
Output is correct |
40 |
Correct |
5 ms |
11100 KB |
Output is correct |
41 |
Correct |
7 ms |
11612 KB |
Output is correct |
42 |
Correct |
3 ms |
2908 KB |
Output is correct |
43 |
Correct |
5 ms |
11612 KB |
Output is correct |
44 |
Correct |
7 ms |
3212 KB |
Output is correct |
45 |
Correct |
69 ms |
5540 KB |
Output is correct |
46 |
Correct |
39 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
2 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
3672 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
8 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
3 ms |
4444 KB |
Output is correct |
33 |
Correct |
13 ms |
4812 KB |
Output is correct |
34 |
Correct |
8 ms |
4700 KB |
Output is correct |
35 |
Correct |
8 ms |
11544 KB |
Output is correct |
36 |
Correct |
2 ms |
9308 KB |
Output is correct |
37 |
Correct |
5 ms |
7452 KB |
Output is correct |
38 |
Correct |
9 ms |
11744 KB |
Output is correct |
39 |
Correct |
5 ms |
10844 KB |
Output is correct |
40 |
Correct |
6 ms |
11100 KB |
Output is correct |
41 |
Correct |
8 ms |
11612 KB |
Output is correct |
42 |
Correct |
3 ms |
2788 KB |
Output is correct |
43 |
Correct |
5 ms |
11612 KB |
Output is correct |
44 |
Correct |
6 ms |
3208 KB |
Output is correct |
45 |
Correct |
69 ms |
5548 KB |
Output is correct |
46 |
Correct |
40 ms |
5464 KB |
Output is correct |
47 |
Correct |
8 ms |
13848 KB |
Output is correct |
48 |
Correct |
5 ms |
9132 KB |
Output is correct |
49 |
Correct |
4 ms |
7256 KB |
Output is correct |
50 |
Correct |
3 ms |
4956 KB |
Output is correct |
51 |
Correct |
34 ms |
90668 KB |
Output is correct |
52 |
Correct |
39 ms |
92440 KB |
Output is correct |
53 |
Correct |
10 ms |
19292 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
6 ms |
3420 KB |
Output is correct |
57 |
Correct |
1 ms |
2652 KB |
Output is correct |
58 |
Correct |
8 ms |
3420 KB |
Output is correct |
59 |
Correct |
5 ms |
2904 KB |
Output is correct |
60 |
Correct |
5 ms |
2908 KB |
Output is correct |
61 |
Correct |
12 ms |
69784 KB |
Output is correct |
62 |
Correct |
18 ms |
25436 KB |
Output is correct |
63 |
Correct |
303 ms |
5296 KB |
Output is correct |
64 |
Correct |
376 ms |
5224 KB |
Output is correct |
65 |
Correct |
566 ms |
5408 KB |
Output is correct |
66 |
Correct |
957 ms |
5464 KB |
Output is correct |
67 |
Correct |
531 ms |
5468 KB |
Output is correct |