#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 30000 + 5;
const ll INF = 1e9;
vector<pair<int, int>> g[MAX_N];
set<int> jumps[MAX_N];
ll dist[MAX_N];
bool used[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int s, t;
for (int i = 0; i < m; ++i) {
int v, k;
cin >> v >> k;
if (i == 0) s = v;
if (i == 1) t = v;
jumps[v].insert(k);
}
for (int v = 1; v < n; ++v) {
for (auto k : jumps[v]) {
for (int u = v - k; u >= 0; u -= k) {
int w = (v - u) / k;
g[v].emplace_back(u, w);
if (jumps[u].find(k) != jumps[u].end()) {
break;
}
}
}
}
for (int v = n - 1; v >= 0; --v) {
for (auto k : jumps[v]) {
for (int u = v + k; u < n; u += k) {
int w = (u - v) / k;
g[v].emplace_back(u, w);
if (jumps[u].find(k) != jumps[u].end()) {
break;
}
}
}
}
fill_n(dist, n, INF);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
auto [curr_dist, v] = pq.top();
pq.pop();
if (used[v]) continue;
used[v] = true;
dist[v] = curr_dist;
if (v == t) break;
if (dist[v] == INF) break;
for (const auto& [u, w] : g[v]) {
if (used[u] || curr_dist + w >= dist[u]) continue;
dist[u] = curr_dist + w;
pq.emplace(dist[u], u);
}
}
if (!used[t]) {
cout << "-1\n";
} else {
cout << dist[t] << '\n';
}
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:69:9: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | if (v == t) break;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2440 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2440 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2452 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2448 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2444 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2448 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2580 KB |
Output is correct |
24 |
Correct |
2 ms |
2760 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2588 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
3476 KB |
Output is correct |
30 |
Correct |
3 ms |
2900 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
2852 KB |
Output is correct |
33 |
Correct |
6 ms |
4176 KB |
Output is correct |
34 |
Correct |
5 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2440 KB |
Output is correct |
2 |
Correct |
2 ms |
2448 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2448 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2516 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2516 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2520 KB |
Output is correct |
21 |
Correct |
2 ms |
2520 KB |
Output is correct |
22 |
Correct |
2 ms |
2456 KB |
Output is correct |
23 |
Correct |
2 ms |
2520 KB |
Output is correct |
24 |
Correct |
3 ms |
2768 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2860 KB |
Output is correct |
29 |
Correct |
4 ms |
3476 KB |
Output is correct |
30 |
Correct |
3 ms |
2828 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
6 ms |
4176 KB |
Output is correct |
34 |
Correct |
5 ms |
4176 KB |
Output is correct |
35 |
Correct |
14 ms |
4956 KB |
Output is correct |
36 |
Correct |
3 ms |
2900 KB |
Output is correct |
37 |
Correct |
16 ms |
5716 KB |
Output is correct |
38 |
Correct |
19 ms |
5972 KB |
Output is correct |
39 |
Correct |
20 ms |
5836 KB |
Output is correct |
40 |
Correct |
20 ms |
5820 KB |
Output is correct |
41 |
Correct |
20 ms |
5948 KB |
Output is correct |
42 |
Correct |
5 ms |
2772 KB |
Output is correct |
43 |
Correct |
5 ms |
2704 KB |
Output is correct |
44 |
Correct |
5 ms |
2708 KB |
Output is correct |
45 |
Correct |
22 ms |
8916 KB |
Output is correct |
46 |
Correct |
22 ms |
8988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2444 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2448 KB |
Output is correct |
9 |
Correct |
1 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2440 KB |
Output is correct |
11 |
Correct |
2 ms |
2580 KB |
Output is correct |
12 |
Correct |
2 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2452 KB |
Output is correct |
14 |
Correct |
2 ms |
2516 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2448 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2584 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2584 KB |
Output is correct |
21 |
Correct |
2 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2516 KB |
Output is correct |
23 |
Correct |
2 ms |
2516 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2580 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
2516 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
3472 KB |
Output is correct |
30 |
Correct |
2 ms |
2824 KB |
Output is correct |
31 |
Correct |
3 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
6 ms |
4176 KB |
Output is correct |
34 |
Correct |
5 ms |
4240 KB |
Output is correct |
35 |
Correct |
15 ms |
4988 KB |
Output is correct |
36 |
Correct |
4 ms |
2836 KB |
Output is correct |
37 |
Correct |
16 ms |
5708 KB |
Output is correct |
38 |
Correct |
19 ms |
5920 KB |
Output is correct |
39 |
Correct |
19 ms |
5768 KB |
Output is correct |
40 |
Correct |
19 ms |
5840 KB |
Output is correct |
41 |
Correct |
19 ms |
5936 KB |
Output is correct |
42 |
Correct |
6 ms |
2788 KB |
Output is correct |
43 |
Correct |
6 ms |
2708 KB |
Output is correct |
44 |
Correct |
5 ms |
2772 KB |
Output is correct |
45 |
Correct |
21 ms |
8948 KB |
Output is correct |
46 |
Correct |
20 ms |
8920 KB |
Output is correct |
47 |
Correct |
47 ms |
13636 KB |
Output is correct |
48 |
Correct |
16 ms |
6224 KB |
Output is correct |
49 |
Correct |
12 ms |
5332 KB |
Output is correct |
50 |
Correct |
11 ms |
4908 KB |
Output is correct |
51 |
Correct |
28 ms |
8848 KB |
Output is correct |
52 |
Correct |
29 ms |
9316 KB |
Output is correct |
53 |
Correct |
14 ms |
5780 KB |
Output is correct |
54 |
Correct |
3 ms |
3216 KB |
Output is correct |
55 |
Correct |
4 ms |
3904 KB |
Output is correct |
56 |
Correct |
11 ms |
5272 KB |
Output is correct |
57 |
Correct |
14 ms |
11912 KB |
Output is correct |
58 |
Correct |
8 ms |
4108 KB |
Output is correct |
59 |
Correct |
8 ms |
4368 KB |
Output is correct |
60 |
Correct |
10 ms |
5072 KB |
Output is correct |
61 |
Correct |
10 ms |
4564 KB |
Output is correct |
62 |
Correct |
23 ms |
9552 KB |
Output is correct |
63 |
Correct |
63 ms |
28468 KB |
Output is correct |
64 |
Correct |
68 ms |
32160 KB |
Output is correct |
65 |
Correct |
96 ms |
41412 KB |
Output is correct |
66 |
Correct |
166 ms |
71508 KB |
Output is correct |
67 |
Correct |
138 ms |
71556 KB |
Output is correct |