#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 30000;
vector<pair<int, int>> adj[N];
int b[N], p[N];
set<int> inv[N];
int n, m;
void solveTestCase() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
inv[b[i]].insert(p[i]);
}
for (int i = 0; i < n; i++) {
for (int j : inv[i]) {
for (int k = i - j, l = 1; k >= 0; k -= j, l++) {
adj[i].emplace_back(k, l);
if (inv[k].count(j))
break;
}
for (int k = i + j, l = 1; k < n; k += j, l++) {
adj[i].emplace_back(k, l);
if (inv[k].count(j))
break;
}
}
}
int dist[n];
fill(dist, dist + n, 1e18);
dist[b[0]] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(greater<>())> pq;
pq.push({0, b[0]});
set<int> done;
while (!pq.empty()) {
auto temp = pq.top();
pq.pop();
if (done.count(temp.second))
continue;
done.insert(temp.second);
assert(temp.first == dist[temp.second] && temp.first != 1e18);
for (auto i : adj[temp.second]) {
if (dist[i.first] > temp.first + i.second) {
dist[i.first] = temp.first + i.second;
pq.push({dist[i.first], i.first});
}
}
}
cout << (dist[b[1]] == 1e18 ? -1 : dist[b[1]]);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--)
solveTestCase();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
5 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
5 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2432 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
7 ms |
2432 KB |
Output is correct |
5 |
Correct |
5 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
7 ms |
2560 KB |
Output is correct |
14 |
Correct |
7 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2560 KB |
Output is correct |
17 |
Correct |
8 ms |
2944 KB |
Output is correct |
18 |
Correct |
7 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2816 KB |
Output is correct |
26 |
Correct |
7 ms |
2816 KB |
Output is correct |
27 |
Correct |
7 ms |
2816 KB |
Output is correct |
28 |
Correct |
8 ms |
3072 KB |
Output is correct |
29 |
Correct |
10 ms |
4096 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
8 ms |
3456 KB |
Output is correct |
32 |
Correct |
8 ms |
3072 KB |
Output is correct |
33 |
Correct |
13 ms |
5384 KB |
Output is correct |
34 |
Correct |
13 ms |
5256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
5 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
7 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2560 KB |
Output is correct |
17 |
Correct |
7 ms |
2944 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
8 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
7 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2944 KB |
Output is correct |
24 |
Correct |
8 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
26 |
Correct |
7 ms |
2816 KB |
Output is correct |
27 |
Correct |
7 ms |
2688 KB |
Output is correct |
28 |
Correct |
8 ms |
3072 KB |
Output is correct |
29 |
Correct |
10 ms |
4096 KB |
Output is correct |
30 |
Correct |
8 ms |
3072 KB |
Output is correct |
31 |
Correct |
8 ms |
3456 KB |
Output is correct |
32 |
Correct |
8 ms |
3072 KB |
Output is correct |
33 |
Correct |
13 ms |
5256 KB |
Output is correct |
34 |
Correct |
14 ms |
5256 KB |
Output is correct |
35 |
Correct |
30 ms |
6192 KB |
Output is correct |
36 |
Correct |
9 ms |
3200 KB |
Output is correct |
37 |
Correct |
27 ms |
7808 KB |
Output is correct |
38 |
Correct |
33 ms |
7936 KB |
Output is correct |
39 |
Correct |
30 ms |
7936 KB |
Output is correct |
40 |
Correct |
34 ms |
7936 KB |
Output is correct |
41 |
Correct |
32 ms |
8064 KB |
Output is correct |
42 |
Correct |
11 ms |
3456 KB |
Output is correct |
43 |
Correct |
12 ms |
3328 KB |
Output is correct |
44 |
Correct |
11 ms |
3456 KB |
Output is correct |
45 |
Correct |
34 ms |
13180 KB |
Output is correct |
46 |
Correct |
36 ms |
13308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2432 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
6 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2572 KB |
Output is correct |
17 |
Correct |
7 ms |
2944 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
7 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
6 ms |
2560 KB |
Output is correct |
23 |
Correct |
7 ms |
2772 KB |
Output is correct |
24 |
Correct |
8 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2816 KB |
Output is correct |
26 |
Correct |
7 ms |
2816 KB |
Output is correct |
27 |
Correct |
7 ms |
2816 KB |
Output is correct |
28 |
Correct |
8 ms |
3072 KB |
Output is correct |
29 |
Correct |
11 ms |
4096 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
8 ms |
3456 KB |
Output is correct |
32 |
Correct |
8 ms |
3200 KB |
Output is correct |
33 |
Correct |
13 ms |
5384 KB |
Output is correct |
34 |
Correct |
13 ms |
5384 KB |
Output is correct |
35 |
Correct |
24 ms |
6144 KB |
Output is correct |
36 |
Correct |
9 ms |
3200 KB |
Output is correct |
37 |
Correct |
29 ms |
7808 KB |
Output is correct |
38 |
Correct |
31 ms |
7928 KB |
Output is correct |
39 |
Correct |
30 ms |
7936 KB |
Output is correct |
40 |
Correct |
30 ms |
7984 KB |
Output is correct |
41 |
Correct |
31 ms |
7936 KB |
Output is correct |
42 |
Correct |
11 ms |
3456 KB |
Output is correct |
43 |
Correct |
12 ms |
3328 KB |
Output is correct |
44 |
Correct |
11 ms |
3456 KB |
Output is correct |
45 |
Correct |
36 ms |
13172 KB |
Output is correct |
46 |
Correct |
36 ms |
13180 KB |
Output is correct |
47 |
Correct |
68 ms |
21872 KB |
Output is correct |
48 |
Correct |
27 ms |
8832 KB |
Output is correct |
49 |
Correct |
21 ms |
6912 KB |
Output is correct |
50 |
Correct |
18 ms |
6544 KB |
Output is correct |
51 |
Correct |
66 ms |
13452 KB |
Output is correct |
52 |
Correct |
72 ms |
14232 KB |
Output is correct |
53 |
Correct |
35 ms |
8436 KB |
Output is correct |
54 |
Correct |
11 ms |
4092 KB |
Output is correct |
55 |
Correct |
14 ms |
5240 KB |
Output is correct |
56 |
Correct |
26 ms |
7552 KB |
Output is correct |
57 |
Correct |
72 ms |
20584 KB |
Output is correct |
58 |
Correct |
20 ms |
6548 KB |
Output is correct |
59 |
Correct |
23 ms |
7032 KB |
Output is correct |
60 |
Correct |
25 ms |
8312 KB |
Output is correct |
61 |
Correct |
26 ms |
7032 KB |
Output is correct |
62 |
Correct |
45 ms |
15728 KB |
Output is correct |
63 |
Correct |
104 ms |
47848 KB |
Output is correct |
64 |
Correct |
114 ms |
54640 KB |
Output is correct |
65 |
Correct |
150 ms |
74216 KB |
Output is correct |
66 |
Correct |
300 ms |
131032 KB |
Output is correct |
67 |
Correct |
288 ms |
131156 KB |
Output is correct |