#include <bits/stdc++.h>
using namespace std;
const int N = 3e4 + 5;
const int K = 205;
struct item {
int v, p, w;
item(int v, int p, int w):
v(v), p(p), w(w) {}
};
int dis[N]; bool vs[N][K];
vector <int> adj[N];
queue <item> q;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int s, t, n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, p; cin >> v >> p;
adj[v].push_back(p);
if (!i) s = v;
if (i == 1) t = v;
}
memset(dis, -1, sizeof dis);
q.emplace(s, 0, 0);
while (!q.empty()) {
item h = q.front(); q.pop();
if (h.v < 0 || h.v >= n) continue;
if (abs(h.p) < K) {
if (vs[h.v][abs(h.p)]) continue;
vs[h.v][abs(h.p)] = 1;
}
if (dis[h.v] == -1) {
dis[h.v] = h.w;
for (int p : adj[h.v]) {
q.emplace(h.v + p, p, h.w + 1);
q.emplace(h.v - p, -p, h.w + 1);
}
}
q.emplace(h.v + h.p, h.p, h.w + 1);
}
cout << dis[t] << '\n';
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:11:20: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
11 | v(v), p(p), w(w) {}
| ^
skyscraper.cpp:21:9: note: 's' was declared here
21 | int s, t, n, m;
| ^
skyscraper.cpp:47:23: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
47 | cout << dis[t] << '\n';
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
2 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
1 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
18 |
Correct |
2 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1280 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
2 ms |
1536 KB |
Output is correct |
24 |
Correct |
2 ms |
1664 KB |
Output is correct |
25 |
Correct |
2 ms |
1664 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
3 ms |
1664 KB |
Output is correct |
28 |
Correct |
2 ms |
1664 KB |
Output is correct |
29 |
Correct |
3 ms |
1536 KB |
Output is correct |
30 |
Correct |
2 ms |
1536 KB |
Output is correct |
31 |
Correct |
2 ms |
1536 KB |
Output is correct |
32 |
Correct |
2 ms |
1536 KB |
Output is correct |
33 |
Correct |
4 ms |
1664 KB |
Output is correct |
34 |
Correct |
4 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1184 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
1 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
2 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1280 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
2 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
2 ms |
1568 KB |
Output is correct |
24 |
Correct |
2 ms |
1664 KB |
Output is correct |
25 |
Correct |
2 ms |
1664 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
3 ms |
1664 KB |
Output is correct |
29 |
Correct |
3 ms |
1536 KB |
Output is correct |
30 |
Correct |
2 ms |
1536 KB |
Output is correct |
31 |
Correct |
2 ms |
1536 KB |
Output is correct |
32 |
Correct |
2 ms |
1536 KB |
Output is correct |
33 |
Correct |
4 ms |
1664 KB |
Output is correct |
34 |
Correct |
4 ms |
1664 KB |
Output is correct |
35 |
Correct |
11 ms |
2432 KB |
Output is correct |
36 |
Correct |
3 ms |
1536 KB |
Output is correct |
37 |
Correct |
10 ms |
2304 KB |
Output is correct |
38 |
Correct |
12 ms |
2688 KB |
Output is correct |
39 |
Correct |
12 ms |
2688 KB |
Output is correct |
40 |
Correct |
12 ms |
2688 KB |
Output is correct |
41 |
Correct |
12 ms |
2688 KB |
Output is correct |
42 |
Correct |
7 ms |
2560 KB |
Output is correct |
43 |
Correct |
7 ms |
2560 KB |
Output is correct |
44 |
Correct |
7 ms |
2688 KB |
Output is correct |
45 |
Correct |
18 ms |
2660 KB |
Output is correct |
46 |
Correct |
16 ms |
2628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1152 KB |
Output is correct |
5 |
Correct |
1 ms |
1152 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
1 ms |
1152 KB |
Output is correct |
9 |
Correct |
1 ms |
1152 KB |
Output is correct |
10 |
Correct |
1 ms |
1152 KB |
Output is correct |
11 |
Correct |
2 ms |
1280 KB |
Output is correct |
12 |
Correct |
2 ms |
1280 KB |
Output is correct |
13 |
Correct |
1 ms |
1280 KB |
Output is correct |
14 |
Correct |
1 ms |
1280 KB |
Output is correct |
15 |
Correct |
1 ms |
1280 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
2 ms |
1408 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1152 KB |
Output is correct |
20 |
Correct |
2 ms |
1664 KB |
Output is correct |
21 |
Correct |
1 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1152 KB |
Output is correct |
23 |
Correct |
2 ms |
1536 KB |
Output is correct |
24 |
Correct |
2 ms |
1664 KB |
Output is correct |
25 |
Correct |
2 ms |
1664 KB |
Output is correct |
26 |
Correct |
2 ms |
1664 KB |
Output is correct |
27 |
Correct |
2 ms |
1664 KB |
Output is correct |
28 |
Correct |
2 ms |
1664 KB |
Output is correct |
29 |
Correct |
3 ms |
1536 KB |
Output is correct |
30 |
Correct |
2 ms |
1536 KB |
Output is correct |
31 |
Correct |
2 ms |
1536 KB |
Output is correct |
32 |
Correct |
2 ms |
1536 KB |
Output is correct |
33 |
Correct |
4 ms |
1664 KB |
Output is correct |
34 |
Correct |
4 ms |
1664 KB |
Output is correct |
35 |
Correct |
9 ms |
2432 KB |
Output is correct |
36 |
Correct |
3 ms |
1536 KB |
Output is correct |
37 |
Correct |
10 ms |
2348 KB |
Output is correct |
38 |
Correct |
12 ms |
2680 KB |
Output is correct |
39 |
Correct |
12 ms |
2688 KB |
Output is correct |
40 |
Correct |
12 ms |
2688 KB |
Output is correct |
41 |
Correct |
16 ms |
2688 KB |
Output is correct |
42 |
Correct |
9 ms |
2560 KB |
Output is correct |
43 |
Correct |
7 ms |
2560 KB |
Output is correct |
44 |
Correct |
10 ms |
2688 KB |
Output is correct |
45 |
Correct |
16 ms |
2552 KB |
Output is correct |
46 |
Correct |
18 ms |
2628 KB |
Output is correct |
47 |
Correct |
43 ms |
4736 KB |
Output is correct |
48 |
Correct |
9 ms |
1920 KB |
Output is correct |
49 |
Correct |
7 ms |
1792 KB |
Output is correct |
50 |
Correct |
5 ms |
1664 KB |
Output is correct |
51 |
Correct |
27 ms |
8284 KB |
Output is correct |
52 |
Correct |
28 ms |
8320 KB |
Output is correct |
53 |
Correct |
23 ms |
8064 KB |
Output is correct |
54 |
Correct |
6 ms |
7224 KB |
Output is correct |
55 |
Correct |
6 ms |
7168 KB |
Output is correct |
56 |
Correct |
16 ms |
8320 KB |
Output is correct |
57 |
Correct |
25 ms |
7296 KB |
Output is correct |
58 |
Correct |
14 ms |
8192 KB |
Output is correct |
59 |
Correct |
16 ms |
8320 KB |
Output is correct |
60 |
Correct |
17 ms |
8192 KB |
Output is correct |
61 |
Correct |
17 ms |
8260 KB |
Output is correct |
62 |
Correct |
35 ms |
8496 KB |
Output is correct |
63 |
Correct |
48 ms |
7800 KB |
Output is correct |
64 |
Correct |
55 ms |
7800 KB |
Output is correct |
65 |
Correct |
132 ms |
7928 KB |
Output is correct |
66 |
Correct |
201 ms |
8300 KB |
Output is correct |
67 |
Correct |
228 ms |
8204 KB |
Output is correct |