#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
// #define int long long
typedef pair<int, int> T;
const int oo = 1e9 + 7, K = 30;
const int mod = 998244353;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<T>> g(n + 2);
auto ch = [&](int v, int j = 0) { return v + j; };
vector<vector<int>> doge(n + 1);
int start, kon;
for (int i = 0; i < m; i++) {
int v, j;
cin >> v >> j;
if (i == 0) start = ch(v);
if (i == 1) kon = ch(v);
doge[v].emplace_back(j);
}
vector<int> dist(n + 2, oo);
set<T> s;
s.insert({0, start});
dist[start] = 0;
while ((int) s.size()) {
auto [d, v] = *s.begin();
s.erase(s.begin());
if (dist[v] < d) continue;
auto popraw = [&](int what, int from, int c) {
if (dist[what] > dist[from] + c) {
dist[what] = dist[from] + c;
s.insert({dist[what], what});
}
};
int vv = v;
for (auto j : doge[vv]) {
for (int u = vv - j, k = 1; u >= 0; u -= j, k++) {
popraw(ch(u), v, k);
}
for (int u = vv + j, k = 1; u < n; u += j, k++) {
popraw(ch(u), v, k);
}
}
//jakies inne losowe
for (auto [x, c] : g[v]) {
popraw(x, v, c);
}
//krotkie skoki
int j = 0;
if (j != 0) {
int pos = v;
popraw(pos, v, 0);
int u = pos;
if (u >= j) {
popraw(ch(u - j, j), v, 1);
}
if (u + j < n) {
popraw(ch(u + j, j), v, 1);
}
}
}
int ans = dist[kon];
if (ans == oo) ans = -1;
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:27:15: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | dist[start] = 0;
| ^
skyscraper.cpp:65:23: warning: 'kon' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | int ans = dist[kon];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 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 |
1 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 |
1 ms |
344 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 |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
1 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 |
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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
4 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 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 |
8 ms |
860 KB |
Output is correct |
39 |
Correct |
6 ms |
860 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
6 ms |
860 KB |
Output is correct |
42 |
Correct |
41 ms |
604 KB |
Output is correct |
43 |
Correct |
40 ms |
604 KB |
Output is correct |
44 |
Correct |
50 ms |
720 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
5 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
344 KB |
Output is correct |
6 |
Correct |
1 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 |
344 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 |
712 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 |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
3 ms |
1116 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
4 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
4 ms |
700 KB |
Output is correct |
38 |
Correct |
6 ms |
860 KB |
Output is correct |
39 |
Correct |
5 ms |
860 KB |
Output is correct |
40 |
Correct |
6 ms |
860 KB |
Output is correct |
41 |
Correct |
7 ms |
940 KB |
Output is correct |
42 |
Correct |
40 ms |
600 KB |
Output is correct |
43 |
Correct |
41 ms |
600 KB |
Output is correct |
44 |
Correct |
43 ms |
724 KB |
Output is correct |
45 |
Correct |
5 ms |
1116 KB |
Output is correct |
46 |
Correct |
6 ms |
1104 KB |
Output is correct |
47 |
Correct |
15 ms |
3420 KB |
Output is correct |
48 |
Correct |
4 ms |
1892 KB |
Output is correct |
49 |
Correct |
4 ms |
2152 KB |
Output is correct |
50 |
Correct |
3 ms |
2052 KB |
Output is correct |
51 |
Correct |
25 ms |
5736 KB |
Output is correct |
52 |
Correct |
25 ms |
5224 KB |
Output is correct |
53 |
Correct |
11 ms |
4172 KB |
Output is correct |
54 |
Correct |
3 ms |
2664 KB |
Output is correct |
55 |
Correct |
3 ms |
2664 KB |
Output is correct |
56 |
Correct |
535 ms |
4316 KB |
Output is correct |
57 |
Correct |
47 ms |
9296 KB |
Output is correct |
58 |
Correct |
481 ms |
2804 KB |
Output is correct |
59 |
Correct |
554 ms |
2648 KB |
Output is correct |
60 |
Correct |
494 ms |
2900 KB |
Output is correct |
61 |
Correct |
490 ms |
2904 KB |
Output is correct |
62 |
Correct |
8 ms |
2908 KB |
Output is correct |
63 |
Correct |
34 ms |
8220 KB |
Output is correct |
64 |
Correct |
35 ms |
8276 KB |
Output is correct |
65 |
Correct |
45 ms |
9552 KB |
Output is correct |
66 |
Correct |
64 ms |
12884 KB |
Output is correct |
67 |
Correct |
59 ms |
13076 KB |
Output is correct |