#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <bitset>
using namespace std;
const int MAXN = 30100;
const int SQN = 40;
const int INF = 1e9 + 177013;
pair<int, int> dogs[MAXN];
vector<int> jlen[MAXN];
bitset<MAXN> used;
int dist[MAXN][SQN];
void update(int i, int p, int d, set<pair<int, pair<int, int>>>& rdist) {
if (dist[i][p] > d) {
dist[i][p] = d;
rdist.insert({d, {i, p}});
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> dogs[i].first >> dogs[i].second;
jlen[dogs[i].first].push_back(dogs[i].second);
}
int s = dogs[0].first;
int t = dogs[1].first;
for (int i = 0; i < n; ++i) {
for (int d = 0; d < SQN; ++d) {
dist[i][d] = INF;
}
}
dist[s][0] = 0;
set<pair<int, pair<int, int>>> rdist;
rdist.insert({0, {s, 0}});
while (rdist.size()) {
auto [d, ip] = *rdist.begin();
auto [i, p] = ip;
rdist.erase(rdist.begin());
if (dist[i][p] < d)
continue;
update(i, 0, d, rdist);
if (p == 0) {
for (auto np : jlen[i]) {
if (np < SQN) {
update(i, np, d, rdist);
continue;
}
for (int j = i % np; j < n; j += np) {
update(j, 0, d + abs(i - j) / np, rdist);
}
}
} else {
if (i >= p)
update(i - p, p, d + 1, rdist);
if (i + p < n)
update(i + p, p, d + 1, rdist);
}
}
if (dist[t][0] >= INF)
cout << "-1\n";
else
cout << dist[t][0] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1032 KB |
Output is correct |
5 |
Correct |
1 ms |
1036 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
1032 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1032 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1036 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1036 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1044 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1428 KB |
Output is correct |
25 |
Correct |
3 ms |
1496 KB |
Output is correct |
26 |
Correct |
2 ms |
1304 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
3 ms |
1344 KB |
Output is correct |
29 |
Correct |
9 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1288 KB |
Output is correct |
31 |
Correct |
5 ms |
1428 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
12 ms |
1808 KB |
Output is correct |
34 |
Correct |
11 ms |
1804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1036 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1040 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1040 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
1044 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1040 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1036 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1304 KB |
Output is correct |
24 |
Correct |
3 ms |
1472 KB |
Output is correct |
25 |
Correct |
2 ms |
1304 KB |
Output is correct |
26 |
Correct |
2 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
10 ms |
1528 KB |
Output is correct |
30 |
Correct |
3 ms |
1364 KB |
Output is correct |
31 |
Correct |
4 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
12 ms |
1808 KB |
Output is correct |
34 |
Correct |
12 ms |
1748 KB |
Output is correct |
35 |
Correct |
12 ms |
2004 KB |
Output is correct |
36 |
Correct |
3 ms |
1364 KB |
Output is correct |
37 |
Correct |
14 ms |
2004 KB |
Output is correct |
38 |
Correct |
15 ms |
2292 KB |
Output is correct |
39 |
Correct |
15 ms |
2208 KB |
Output is correct |
40 |
Correct |
19 ms |
2260 KB |
Output is correct |
41 |
Correct |
16 ms |
2240 KB |
Output is correct |
42 |
Correct |
7 ms |
2004 KB |
Output is correct |
43 |
Correct |
5 ms |
2004 KB |
Output is correct |
44 |
Correct |
5 ms |
2004 KB |
Output is correct |
45 |
Correct |
17 ms |
2260 KB |
Output is correct |
46 |
Correct |
17 ms |
2324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1032 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1036 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
1044 KB |
Output is correct |
15 |
Correct |
2 ms |
1056 KB |
Output is correct |
16 |
Correct |
1 ms |
1044 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1308 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1244 KB |
Output is correct |
23 |
Correct |
2 ms |
1244 KB |
Output is correct |
24 |
Correct |
3 ms |
1432 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
10 ms |
1424 KB |
Output is correct |
30 |
Correct |
3 ms |
1376 KB |
Output is correct |
31 |
Correct |
5 ms |
1504 KB |
Output is correct |
32 |
Correct |
2 ms |
1504 KB |
Output is correct |
33 |
Correct |
13 ms |
1816 KB |
Output is correct |
34 |
Correct |
13 ms |
1820 KB |
Output is correct |
35 |
Correct |
11 ms |
2016 KB |
Output is correct |
36 |
Correct |
3 ms |
1320 KB |
Output is correct |
37 |
Correct |
20 ms |
2052 KB |
Output is correct |
38 |
Correct |
19 ms |
2228 KB |
Output is correct |
39 |
Correct |
22 ms |
2272 KB |
Output is correct |
40 |
Correct |
16 ms |
2304 KB |
Output is correct |
41 |
Correct |
20 ms |
2272 KB |
Output is correct |
42 |
Correct |
6 ms |
2016 KB |
Output is correct |
43 |
Correct |
5 ms |
1972 KB |
Output is correct |
44 |
Correct |
5 ms |
2016 KB |
Output is correct |
45 |
Correct |
18 ms |
2272 KB |
Output is correct |
46 |
Correct |
18 ms |
2272 KB |
Output is correct |
47 |
Correct |
57 ms |
5344 KB |
Output is correct |
48 |
Correct |
8 ms |
5452 KB |
Output is correct |
49 |
Correct |
8 ms |
5796 KB |
Output is correct |
50 |
Correct |
7 ms |
6052 KB |
Output is correct |
51 |
Correct |
54 ms |
8672 KB |
Output is correct |
52 |
Correct |
52 ms |
8704 KB |
Output is correct |
53 |
Correct |
19 ms |
6892 KB |
Output is correct |
54 |
Correct |
5 ms |
5728 KB |
Output is correct |
55 |
Correct |
7 ms |
5728 KB |
Output is correct |
56 |
Correct |
14 ms |
7136 KB |
Output is correct |
57 |
Correct |
100 ms |
8756 KB |
Output is correct |
58 |
Correct |
13 ms |
6228 KB |
Output is correct |
59 |
Correct |
15 ms |
6356 KB |
Output is correct |
60 |
Correct |
15 ms |
6420 KB |
Output is correct |
61 |
Correct |
16 ms |
6356 KB |
Output is correct |
62 |
Correct |
28 ms |
7300 KB |
Output is correct |
63 |
Correct |
82 ms |
12680 KB |
Output is correct |
64 |
Correct |
68 ms |
12944 KB |
Output is correct |
65 |
Correct |
79 ms |
14644 KB |
Output is correct |
66 |
Correct |
224 ms |
16332 KB |
Output is correct |
67 |
Correct |
337 ms |
16480 KB |
Output is correct |