#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;
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 |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 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 |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 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 |
4896 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
8536 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
8536 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
3 ms |
10584 KB |
Output is correct |
24 |
Correct |
3 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
4696 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
8 ms |
2904 KB |
Output is correct |
30 |
Correct |
2 ms |
2392 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 |
4700 KB |
Output is correct |
34 |
Correct |
13 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
0 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
2 ms |
4696 KB |
Output is correct |
18 |
Correct |
1 ms |
8536 KB |
Output is correct |
19 |
Correct |
1 ms |
8536 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
2 ms |
8536 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
2 ms |
10584 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
3 ms |
10584 KB |
Output is correct |
26 |
Correct |
1 ms |
4696 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
7 ms |
2648 KB |
Output is correct |
30 |
Correct |
2 ms |
2392 KB |
Output is correct |
31 |
Correct |
4 ms |
2396 KB |
Output is correct |
32 |
Correct |
3 ms |
4440 KB |
Output is correct |
33 |
Correct |
13 ms |
4696 KB |
Output is correct |
34 |
Correct |
13 ms |
4696 KB |
Output is correct |
35 |
Correct |
18 ms |
11544 KB |
Output is correct |
36 |
Correct |
4 ms |
10840 KB |
Output is correct |
37 |
Correct |
33 ms |
7448 KB |
Output is correct |
38 |
Correct |
27 ms |
11608 KB |
Output is correct |
39 |
Correct |
27 ms |
11608 KB |
Output is correct |
40 |
Correct |
27 ms |
11608 KB |
Output is correct |
41 |
Correct |
28 ms |
11608 KB |
Output is correct |
42 |
Correct |
7 ms |
5464 KB |
Output is correct |
43 |
Correct |
7 ms |
11612 KB |
Output is correct |
44 |
Correct |
6 ms |
3204 KB |
Output is correct |
45 |
Correct |
71 ms |
5460 KB |
Output is correct |
46 |
Correct |
69 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4696 KB |
Output is correct |
12 |
Correct |
2 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
2 ms |
4696 KB |
Output is correct |
15 |
Correct |
2 ms |
4696 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
3 ms |
4696 KB |
Output is correct |
18 |
Correct |
2 ms |
8536 KB |
Output is correct |
19 |
Correct |
1 ms |
8536 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 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 |
10784 KB |
Output is correct |
24 |
Correct |
3 ms |
10840 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
4696 KB |
Output is correct |
27 |
Correct |
2 ms |
10840 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
7 ms |
2648 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 |
4440 KB |
Output is correct |
33 |
Correct |
13 ms |
4700 KB |
Output is correct |
34 |
Correct |
14 ms |
4696 KB |
Output is correct |
35 |
Correct |
18 ms |
11544 KB |
Output is correct |
36 |
Correct |
4 ms |
10840 KB |
Output is correct |
37 |
Correct |
27 ms |
7448 KB |
Output is correct |
38 |
Correct |
27 ms |
11608 KB |
Output is correct |
39 |
Correct |
27 ms |
11608 KB |
Output is correct |
40 |
Correct |
28 ms |
11676 KB |
Output is correct |
41 |
Correct |
27 ms |
11608 KB |
Output is correct |
42 |
Correct |
6 ms |
5464 KB |
Output is correct |
43 |
Correct |
8 ms |
11608 KB |
Output is correct |
44 |
Correct |
6 ms |
3400 KB |
Output is correct |
45 |
Correct |
67 ms |
5460 KB |
Output is correct |
46 |
Correct |
68 ms |
5460 KB |
Output is correct |
47 |
Correct |
99 ms |
14164 KB |
Output is correct |
48 |
Correct |
5 ms |
9048 KB |
Output is correct |
49 |
Correct |
4 ms |
7000 KB |
Output is correct |
50 |
Correct |
3 ms |
4956 KB |
Output is correct |
51 |
Correct |
56 ms |
112300 KB |
Output is correct |
52 |
Correct |
60 ms |
112320 KB |
Output is correct |
53 |
Correct |
12 ms |
21848 KB |
Output is correct |
54 |
Correct |
1 ms |
2392 KB |
Output is correct |
55 |
Correct |
1 ms |
2392 KB |
Output is correct |
56 |
Correct |
6 ms |
3416 KB |
Output is correct |
57 |
Correct |
47 ms |
4696 KB |
Output is correct |
58 |
Correct |
7 ms |
5464 KB |
Output is correct |
59 |
Correct |
8 ms |
5524 KB |
Output is correct |
60 |
Correct |
10 ms |
5524 KB |
Output is correct |
61 |
Correct |
21 ms |
112024 KB |
Output is correct |
62 |
Correct |
18 ms |
32600 KB |
Output is correct |
63 |
Correct |
301 ms |
5260 KB |
Output is correct |
64 |
Correct |
373 ms |
5400 KB |
Output is correct |
65 |
Correct |
566 ms |
5712 KB |
Output is correct |
66 |
Correct |
963 ms |
5716 KB |
Output is correct |
67 |
Correct |
949 ms |
5720 KB |
Output is correct |