#include <bits/stdc++.h>
#define nl '\n'
const int mxN = 30006;
std::set<int> doges[mxN];
int B[mxN], P[mxN];
int N, M;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> N >> M;
for (int i = 0; i < M; i++) {
std::cin >> B[i] >> P[i];
doges[B[i]].insert(P[i]);
}
if (B[0] == B[1]) {
std::cout << 0 << nl;
return 0;
}
auto dijkstra = [&](int s) -> int {
std::priority_queue<std::pair<int, int>> pq;
std::vector<int> dp(N, 1e9);
dp[s] = 0;
pq.push({0, s});
while (pq.size()) {
auto [d, u] = pq.top();
d = -d;
pq.pop();
if (dp[u] < d) continue;
for (int p : doges[u]) {
for (int w = 1; u + w * p < N; w++) {
int to = u + w * p;
auto dd = dp[u] + w;
if (dd < dp[to]) {
dp[to] = dd;
pq.emplace(-dp[to], to);
}
if (doges[to].find(p) != doges[to].end()) break;
}
for (int w = 1; u - w * p >= 0; w++) {
int to = u - w * p;
auto dd = dp[u] + w;
if (dd < dp[to]) {
dp[to] = dd;
pq.emplace(-dp[to], to);
}
if (doges[to].find(p) != doges[to].end()) break;
}
}
}
return dp[B[1]] < 1e9 ? dp[B[1]] : -1;
};
std::cout << dijkstra(B[0]) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1780 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1680 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
2136 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
2 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1624 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1880 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2080 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1884 KB |
Output is correct |
30 |
Correct |
2 ms |
1880 KB |
Output is correct |
31 |
Correct |
2 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
3 ms |
2392 KB |
Output is correct |
34 |
Correct |
3 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1880 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
1884 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
1 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1880 KB |
Output is correct |
29 |
Correct |
2 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1880 KB |
Output is correct |
33 |
Correct |
3 ms |
2136 KB |
Output is correct |
34 |
Correct |
3 ms |
2140 KB |
Output is correct |
35 |
Correct |
10 ms |
3164 KB |
Output is correct |
36 |
Correct |
2 ms |
2140 KB |
Output is correct |
37 |
Correct |
11 ms |
3040 KB |
Output is correct |
38 |
Correct |
14 ms |
3416 KB |
Output is correct |
39 |
Correct |
14 ms |
3420 KB |
Output is correct |
40 |
Correct |
13 ms |
3620 KB |
Output is correct |
41 |
Correct |
13 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
1992 KB |
Output is correct |
43 |
Correct |
4 ms |
2140 KB |
Output is correct |
44 |
Correct |
4 ms |
2128 KB |
Output is correct |
45 |
Correct |
12 ms |
3928 KB |
Output is correct |
46 |
Correct |
12 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1624 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
1888 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1884 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
2 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
1 ms |
1884 KB |
Output is correct |
20 |
Correct |
1 ms |
1884 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
1 ms |
1884 KB |
Output is correct |
23 |
Correct |
1 ms |
1884 KB |
Output is correct |
24 |
Correct |
2 ms |
2216 KB |
Output is correct |
25 |
Correct |
2 ms |
1884 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
2 ms |
1884 KB |
Output is correct |
28 |
Correct |
1 ms |
1884 KB |
Output is correct |
29 |
Correct |
2 ms |
1884 KB |
Output is correct |
30 |
Correct |
1 ms |
1884 KB |
Output is correct |
31 |
Correct |
1 ms |
1884 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
3 ms |
2140 KB |
Output is correct |
34 |
Correct |
3 ms |
2140 KB |
Output is correct |
35 |
Correct |
10 ms |
3160 KB |
Output is correct |
36 |
Correct |
2 ms |
2140 KB |
Output is correct |
37 |
Correct |
11 ms |
2808 KB |
Output is correct |
38 |
Correct |
14 ms |
3420 KB |
Output is correct |
39 |
Correct |
14 ms |
3612 KB |
Output is correct |
40 |
Correct |
14 ms |
3420 KB |
Output is correct |
41 |
Correct |
13 ms |
3416 KB |
Output is correct |
42 |
Correct |
4 ms |
1884 KB |
Output is correct |
43 |
Correct |
4 ms |
2004 KB |
Output is correct |
44 |
Correct |
4 ms |
2148 KB |
Output is correct |
45 |
Correct |
12 ms |
3676 KB |
Output is correct |
46 |
Correct |
12 ms |
3716 KB |
Output is correct |
47 |
Correct |
24 ms |
3800 KB |
Output is correct |
48 |
Correct |
5 ms |
3164 KB |
Output is correct |
49 |
Correct |
5 ms |
3160 KB |
Output is correct |
50 |
Correct |
4 ms |
2652 KB |
Output is correct |
51 |
Correct |
29 ms |
4264 KB |
Output is correct |
52 |
Correct |
24 ms |
4056 KB |
Output is correct |
53 |
Correct |
13 ms |
4148 KB |
Output is correct |
54 |
Correct |
2 ms |
2140 KB |
Output is correct |
55 |
Correct |
2 ms |
2140 KB |
Output is correct |
56 |
Correct |
6 ms |
3840 KB |
Output is correct |
57 |
Correct |
19 ms |
2776 KB |
Output is correct |
58 |
Correct |
5 ms |
2396 KB |
Output is correct |
59 |
Correct |
6 ms |
2652 KB |
Output is correct |
60 |
Correct |
6 ms |
2652 KB |
Output is correct |
61 |
Correct |
7 ms |
2840 KB |
Output is correct |
62 |
Correct |
9 ms |
3932 KB |
Output is correct |
63 |
Correct |
29 ms |
4820 KB |
Output is correct |
64 |
Correct |
28 ms |
4816 KB |
Output is correct |
65 |
Correct |
35 ms |
4820 KB |
Output is correct |
66 |
Correct |
57 ms |
5840 KB |
Output is correct |
67 |
Correct |
60 ms |
5840 KB |
Output is correct |