/**
* author: NimaAryan
* created: 2023-11-17 08:21:07
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
template <typename T = i64>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T = i64>
using max_heap = priority_queue<T, vector<T>, less<T>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<int> B(M), P(M);
for (int i = 0; i < M; ++i) {
cin >> B[i] >> P[i];
}
vector<vector<int>> at(N);
for (int i = 0; i < M; ++i) {
at[B[i]].push_back(P[i]);
}
constexpr int inf = numeric_limits<int>::max();
vector<int> dist(N, inf);
vector<bool> vis(N);
min_heap<pair<int, int>> h;
auto add = [&](int v, int c) {
if (c < dist[v]) {
h.emplace(dist[v] = c, v);
}
};
add(B[0], 0);
while (!h.empty()) {
auto [cd, v] = h.top();
h.pop();
if (cd > dist[v]) {
continue;
}
vis[v] = true;
for (int p : at[v]) {
for (int t = 1; v + t * p < N; ++t) {
int u = v + t * p;
add(u, cd + t);
}
for (int t = 1; v - t * p >= 0; ++t) {
int u = v - t * p;
add(u, cd + t);
}
}
}
cout << (vis[B[1]] ? dist[B[1]] : -1) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
2 ms |
600 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
352 KB |
Output is correct |
20 |
Correct |
3 ms |
616 KB |
Output is correct |
21 |
Correct |
0 ms |
360 KB |
Output is correct |
22 |
Correct |
0 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
360 KB |
Output is correct |
25 |
Correct |
1 ms |
612 KB |
Output is correct |
26 |
Correct |
3 ms |
352 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
352 KB |
Output is correct |
29 |
Correct |
2 ms |
608 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
4 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
6 ms |
860 KB |
Output is correct |
38 |
Correct |
5 ms |
1044 KB |
Output is correct |
39 |
Correct |
8 ms |
860 KB |
Output is correct |
40 |
Correct |
5 ms |
1008 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
41 ms |
860 KB |
Output is correct |
43 |
Correct |
40 ms |
856 KB |
Output is correct |
44 |
Correct |
41 ms |
860 KB |
Output is correct |
45 |
Correct |
5 ms |
1368 KB |
Output is correct |
46 |
Correct |
5 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
4 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
4 ms |
860 KB |
Output is correct |
38 |
Correct |
5 ms |
1112 KB |
Output is correct |
39 |
Correct |
5 ms |
940 KB |
Output is correct |
40 |
Correct |
6 ms |
1112 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
41 ms |
952 KB |
Output is correct |
43 |
Correct |
40 ms |
860 KB |
Output is correct |
44 |
Correct |
40 ms |
860 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
6 ms |
1112 KB |
Output is correct |
47 |
Correct |
12 ms |
1752 KB |
Output is correct |
48 |
Correct |
4 ms |
1660 KB |
Output is correct |
49 |
Correct |
5 ms |
1628 KB |
Output is correct |
50 |
Correct |
4 ms |
1624 KB |
Output is correct |
51 |
Correct |
19 ms |
3124 KB |
Output is correct |
52 |
Correct |
23 ms |
2816 KB |
Output is correct |
53 |
Correct |
10 ms |
2524 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1372 KB |
Output is correct |
56 |
Correct |
579 ms |
2776 KB |
Output is correct |
57 |
Correct |
34 ms |
3536 KB |
Output is correct |
58 |
Correct |
572 ms |
1884 KB |
Output is correct |
59 |
Correct |
568 ms |
2136 KB |
Output is correct |
60 |
Correct |
591 ms |
2132 KB |
Output is correct |
61 |
Correct |
504 ms |
1884 KB |
Output is correct |
62 |
Correct |
9 ms |
2424 KB |
Output is correct |
63 |
Correct |
23 ms |
2772 KB |
Output is correct |
64 |
Correct |
24 ms |
3796 KB |
Output is correct |
65 |
Correct |
28 ms |
3880 KB |
Output is correct |
66 |
Correct |
40 ms |
3800 KB |
Output is correct |
67 |
Correct |
41 ms |
3792 KB |
Output is correct |