#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>
#include <unordered_set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
const ll SZ = 30002, INF = 1e9 + 10;
ll n, m;
unordered_set<ll> us;
ll dist[SZ];
bool used[SZ];
vector<ll> cur[SZ];
bitset<SZ> bts[SZ];
int main()
{
fastInp;
cin >> n >> m;
ll ind = 0, ind2 = 0, b2 = 0;
for (int i = 0; i < m; i++) {
ll b, p;
cin >> b >> p;
cur[b].push_back(p);
if (i == 0) {
ind2 = b;
b2 = p;
}
if (i == 1) {
ind = b;
}
}
queue<pair<ll, ll>> q;
queue<ll> q2;
q.push({ ind2, b2 });
for (int i = 0; i < n; i++) dist[i] = INF;
dist[ind2] = 0;
q2.push(0);
swap(n, m);
while (!q.empty()) {
ll v = q.front().first;
pair<ll, ll> u = q.front();
ll d = q2.front();
q2.pop();
q.pop();
ll c = u.second;
if (v - c >= 0 && v - c < m && !bts[v - c][c]) {
if (v - c >= 0 && v - c < m) {
bts[v - c][c] = 1;
dist[v - c] = min(dist[v - c], d + 1);
q.push({ v - c, c });
q2.push({ d + 1 });
}
}
if (v + c >= 0 && v + c < m && !bts[v + c][c]) {
if (v + c >= 0 && v + c < m) {
bts[v + c][c] = 1;
dist[v + c] = min(dist[v + c], d + 1);
q.push({ v + c, c });
q2.push({ d + 1 });
}
}
if (!used[v]) {
used[v] = 1;
for (auto c : cur[v]) {
if (v - c >= 0 && v - c < m && !bts[v - c][c]) {
if (v - c >= 0 && v - c < m) {
bts[v - c][c] = 1;
dist[v - c] = min(dist[v - c], d + 1);
q.push({ v - c, c });
q2.push({ d + 1 });
}
}
if (v + c >= 0 && v + c < m && !bts[v + c][c]) {
if (v + c >= 0 && v + c < m) {
bts[v + c][c] = 1;
dist[v + c] = min(dist[v + c], d + 1);
q.push({ v + c, c });
q2.push({ d + 1 });
}
}
}
}
if (dist[ind] != INF) {
cout << dist[ind];
return 0;
}
}
cout << "-1";
return 0;
}
/*
5
1 4 1 3 5
2 1 4 1 2
5 1 1 4 5
3 5 1 1 2
4 3 5 1 1
3
1 1 1
2 2 2
3 3 3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
2 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1516 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1516 KB |
Output is correct |
15 |
Correct |
2 ms |
1516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
2 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1388 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1516 KB |
Output is correct |
15 |
Correct |
2 ms |
1516 KB |
Output is correct |
16 |
Correct |
1 ms |
1136 KB |
Output is correct |
17 |
Correct |
3 ms |
3436 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
7 ms |
8556 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1132 KB |
Output is correct |
23 |
Correct |
5 ms |
6380 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7788 KB |
Output is correct |
28 |
Correct |
9 ms |
8556 KB |
Output is correct |
29 |
Correct |
9 ms |
8428 KB |
Output is correct |
30 |
Correct |
7 ms |
8428 KB |
Output is correct |
31 |
Correct |
8 ms |
8556 KB |
Output is correct |
32 |
Correct |
8 ms |
8428 KB |
Output is correct |
33 |
Correct |
12 ms |
8556 KB |
Output is correct |
34 |
Correct |
9 ms |
8428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1420 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1516 KB |
Output is correct |
15 |
Correct |
2 ms |
1516 KB |
Output is correct |
16 |
Correct |
1 ms |
1132 KB |
Output is correct |
17 |
Correct |
3 ms |
3436 KB |
Output is correct |
18 |
Correct |
2 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
7 ms |
8556 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1132 KB |
Output is correct |
23 |
Correct |
5 ms |
6380 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
7 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7788 KB |
Output is correct |
28 |
Correct |
8 ms |
8556 KB |
Output is correct |
29 |
Correct |
9 ms |
8428 KB |
Output is correct |
30 |
Correct |
7 ms |
8428 KB |
Output is correct |
31 |
Correct |
8 ms |
8428 KB |
Output is correct |
32 |
Correct |
7 ms |
8428 KB |
Output is correct |
33 |
Correct |
12 ms |
8556 KB |
Output is correct |
34 |
Correct |
9 ms |
8428 KB |
Output is correct |
35 |
Correct |
11 ms |
7148 KB |
Output is correct |
36 |
Correct |
3 ms |
2156 KB |
Output is correct |
37 |
Correct |
9 ms |
5996 KB |
Output is correct |
38 |
Correct |
13 ms |
7780 KB |
Output is correct |
39 |
Correct |
8 ms |
1900 KB |
Output is correct |
40 |
Correct |
8 ms |
2028 KB |
Output is correct |
41 |
Correct |
9 ms |
3180 KB |
Output is correct |
42 |
Correct |
10 ms |
7916 KB |
Output is correct |
43 |
Correct |
11 ms |
8300 KB |
Output is correct |
44 |
Correct |
11 ms |
8980 KB |
Output is correct |
45 |
Correct |
35 ms |
10468 KB |
Output is correct |
46 |
Correct |
21 ms |
10476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
1 ms |
1132 KB |
Output is correct |
12 |
Correct |
2 ms |
1388 KB |
Output is correct |
13 |
Correct |
2 ms |
1516 KB |
Output is correct |
14 |
Correct |
2 ms |
1516 KB |
Output is correct |
15 |
Correct |
2 ms |
1516 KB |
Output is correct |
16 |
Correct |
2 ms |
1124 KB |
Output is correct |
17 |
Correct |
3 ms |
3436 KB |
Output is correct |
18 |
Correct |
2 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
7 ms |
8556 KB |
Output is correct |
21 |
Correct |
1 ms |
1132 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
5 ms |
6380 KB |
Output is correct |
24 |
Correct |
7 ms |
7532 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
6 ms |
7404 KB |
Output is correct |
27 |
Correct |
6 ms |
7788 KB |
Output is correct |
28 |
Correct |
8 ms |
8556 KB |
Output is correct |
29 |
Correct |
9 ms |
8428 KB |
Output is correct |
30 |
Correct |
7 ms |
8428 KB |
Output is correct |
31 |
Correct |
8 ms |
8428 KB |
Output is correct |
32 |
Correct |
8 ms |
8428 KB |
Output is correct |
33 |
Correct |
12 ms |
8556 KB |
Output is correct |
34 |
Correct |
9 ms |
8428 KB |
Output is correct |
35 |
Correct |
11 ms |
7148 KB |
Output is correct |
36 |
Correct |
3 ms |
2156 KB |
Output is correct |
37 |
Correct |
9 ms |
5996 KB |
Output is correct |
38 |
Correct |
13 ms |
7916 KB |
Output is correct |
39 |
Correct |
8 ms |
1900 KB |
Output is correct |
40 |
Correct |
8 ms |
2028 KB |
Output is correct |
41 |
Correct |
9 ms |
3052 KB |
Output is correct |
42 |
Correct |
11 ms |
7916 KB |
Output is correct |
43 |
Correct |
11 ms |
8300 KB |
Output is correct |
44 |
Correct |
11 ms |
8980 KB |
Output is correct |
45 |
Correct |
33 ms |
10476 KB |
Output is correct |
46 |
Correct |
21 ms |
10496 KB |
Output is correct |
47 |
Correct |
16 ms |
13420 KB |
Output is correct |
48 |
Correct |
7 ms |
1932 KB |
Output is correct |
49 |
Correct |
7 ms |
2028 KB |
Output is correct |
50 |
Correct |
5 ms |
1772 KB |
Output is correct |
51 |
Correct |
71 ms |
74468 KB |
Output is correct |
52 |
Correct |
80 ms |
84196 KB |
Output is correct |
53 |
Correct |
21 ms |
17900 KB |
Output is correct |
54 |
Correct |
44 ms |
61540 KB |
Output is correct |
55 |
Correct |
58 ms |
78180 KB |
Output is correct |
56 |
Correct |
92 ms |
112616 KB |
Output is correct |
57 |
Correct |
3 ms |
2924 KB |
Output is correct |
58 |
Correct |
88 ms |
111972 KB |
Output is correct |
59 |
Correct |
87 ms |
104320 KB |
Output is correct |
60 |
Correct |
86 ms |
104420 KB |
Output is correct |
61 |
Correct |
82 ms |
99300 KB |
Output is correct |
62 |
Correct |
138 ms |
112740 KB |
Output is correct |
63 |
Correct |
311 ms |
112612 KB |
Output is correct |
64 |
Correct |
366 ms |
112484 KB |
Output is correct |
65 |
Correct |
475 ms |
112740 KB |
Output is correct |
66 |
Correct |
726 ms |
113520 KB |
Output is correct |
67 |
Correct |
396 ms |
113636 KB |
Output is correct |