#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int d[200005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int s, t;
for (int i = 1; i <= m; ++i) {
int b, p;
cin >> b >> p;
b++;
if (i == 1) {
s = b;
} else if (i == 2) {
t = b;
}
g[b].emplace_back(p);
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
memset(d, 0x3f, sizeof d);
d[s] = 0;
q.emplace(d[s], s);
int ans = -1;
while (q.size()) {
int u = q.top().second, dl = q.top().first;
q.pop();
if (dl > d[u]) {
continue;
}
if (u == t) {
ans = dl;
break;
}
for (int i: g[u]) {
for (int j = 1; u + j * i <= n; ++j) {
int v = u + j * i;
if (d[v] > d[u] + j) {
d[v] = d[u] + j;
q.emplace(d[v], v);
}
}
for (int j = 1; u - j * i >= 1; ++j) {
int v = u - j * i;
if (d[v] > d[u] + j) {
d[v] = d[u] + j;
q.emplace(d[v], v);
}
}
}
}
cout << ans << '\n';
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:54:3: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
54 | if (u == t) {
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
4 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
4 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5768 KB |
Output is correct |
7 |
Correct |
4 ms |
5728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
4 ms |
5724 KB |
Output is correct |
3 |
Correct |
4 ms |
5724 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
5928 KB |
Output is correct |
9 |
Correct |
3 ms |
5724 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
4 ms |
5724 KB |
Output is correct |
12 |
Correct |
4 ms |
5720 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5724 KB |
Output is correct |
15 |
Correct |
3 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
4 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5932 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5724 KB |
Output is correct |
10 |
Correct |
3 ms |
5932 KB |
Output is correct |
11 |
Correct |
4 ms |
5724 KB |
Output is correct |
12 |
Correct |
4 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5944 KB |
Output is correct |
15 |
Correct |
2 ms |
5928 KB |
Output is correct |
16 |
Correct |
3 ms |
5724 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
4 ms |
5724 KB |
Output is correct |
19 |
Correct |
4 ms |
5724 KB |
Output is correct |
20 |
Correct |
10 ms |
6044 KB |
Output is correct |
21 |
Correct |
3 ms |
5724 KB |
Output is correct |
22 |
Correct |
2 ms |
5724 KB |
Output is correct |
23 |
Correct |
3 ms |
6232 KB |
Output is correct |
24 |
Correct |
3 ms |
5944 KB |
Output is correct |
25 |
Correct |
2 ms |
5772 KB |
Output is correct |
26 |
Correct |
3 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5932 KB |
Output is correct |
30 |
Correct |
3 ms |
5980 KB |
Output is correct |
31 |
Correct |
5 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
5808 KB |
Output is correct |
33 |
Correct |
4 ms |
5980 KB |
Output is correct |
34 |
Correct |
4 ms |
6200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5720 KB |
Output is correct |
2 |
Correct |
4 ms |
5720 KB |
Output is correct |
3 |
Correct |
3 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
3 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5724 KB |
Output is correct |
9 |
Correct |
2 ms |
5724 KB |
Output is correct |
10 |
Correct |
3 ms |
5724 KB |
Output is correct |
11 |
Correct |
4 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5724 KB |
Output is correct |
13 |
Correct |
3 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5724 KB |
Output is correct |
15 |
Correct |
3 ms |
5724 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
3 ms |
5984 KB |
Output is correct |
18 |
Correct |
3 ms |
5780 KB |
Output is correct |
19 |
Correct |
4 ms |
6184 KB |
Output is correct |
20 |
Correct |
6 ms |
6048 KB |
Output is correct |
21 |
Correct |
3 ms |
5728 KB |
Output is correct |
22 |
Correct |
3 ms |
5728 KB |
Output is correct |
23 |
Correct |
4 ms |
5852 KB |
Output is correct |
24 |
Correct |
3 ms |
5988 KB |
Output is correct |
25 |
Correct |
2 ms |
5988 KB |
Output is correct |
26 |
Correct |
3 ms |
5960 KB |
Output is correct |
27 |
Correct |
4 ms |
5992 KB |
Output is correct |
28 |
Correct |
3 ms |
5992 KB |
Output is correct |
29 |
Correct |
5 ms |
5948 KB |
Output is correct |
30 |
Correct |
4 ms |
5984 KB |
Output is correct |
31 |
Correct |
5 ms |
5992 KB |
Output is correct |
32 |
Correct |
3 ms |
6028 KB |
Output is correct |
33 |
Correct |
5 ms |
5980 KB |
Output is correct |
34 |
Correct |
5 ms |
5980 KB |
Output is correct |
35 |
Correct |
5 ms |
6284 KB |
Output is correct |
36 |
Correct |
4 ms |
6028 KB |
Output is correct |
37 |
Correct |
7 ms |
6168 KB |
Output is correct |
38 |
Correct |
6 ms |
6492 KB |
Output is correct |
39 |
Correct |
6 ms |
6236 KB |
Output is correct |
40 |
Correct |
6 ms |
6232 KB |
Output is correct |
41 |
Correct |
6 ms |
6492 KB |
Output is correct |
42 |
Correct |
4 ms |
6192 KB |
Output is correct |
43 |
Correct |
4 ms |
6236 KB |
Output is correct |
44 |
Correct |
45 ms |
6252 KB |
Output is correct |
45 |
Correct |
8 ms |
6344 KB |
Output is correct |
46 |
Correct |
7 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
2 ms |
5724 KB |
Output is correct |
3 |
Correct |
3 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5852 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5724 KB |
Output is correct |
9 |
Correct |
3 ms |
5800 KB |
Output is correct |
10 |
Correct |
4 ms |
5800 KB |
Output is correct |
11 |
Correct |
3 ms |
5724 KB |
Output is correct |
12 |
Correct |
3 ms |
5724 KB |
Output is correct |
13 |
Correct |
4 ms |
5724 KB |
Output is correct |
14 |
Correct |
2 ms |
5724 KB |
Output is correct |
15 |
Correct |
3 ms |
5724 KB |
Output is correct |
16 |
Correct |
3 ms |
5816 KB |
Output is correct |
17 |
Correct |
4 ms |
5980 KB |
Output is correct |
18 |
Correct |
4 ms |
5940 KB |
Output is correct |
19 |
Correct |
3 ms |
5724 KB |
Output is correct |
20 |
Correct |
6 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5952 KB |
Output is correct |
22 |
Correct |
3 ms |
5724 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5988 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
4 ms |
5736 KB |
Output is correct |
28 |
Correct |
3 ms |
5992 KB |
Output is correct |
29 |
Correct |
5 ms |
6172 KB |
Output is correct |
30 |
Correct |
4 ms |
5988 KB |
Output is correct |
31 |
Correct |
4 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
5980 KB |
Output is correct |
33 |
Correct |
5 ms |
5980 KB |
Output is correct |
34 |
Correct |
4 ms |
5980 KB |
Output is correct |
35 |
Correct |
6 ms |
6328 KB |
Output is correct |
36 |
Correct |
5 ms |
5936 KB |
Output is correct |
37 |
Correct |
5 ms |
6348 KB |
Output is correct |
38 |
Correct |
7 ms |
6492 KB |
Output is correct |
39 |
Correct |
6 ms |
6196 KB |
Output is correct |
40 |
Correct |
6 ms |
6236 KB |
Output is correct |
41 |
Correct |
6 ms |
6492 KB |
Output is correct |
42 |
Correct |
5 ms |
6236 KB |
Output is correct |
43 |
Correct |
7 ms |
6364 KB |
Output is correct |
44 |
Correct |
44 ms |
6236 KB |
Output is correct |
45 |
Correct |
6 ms |
6488 KB |
Output is correct |
46 |
Correct |
7 ms |
6488 KB |
Output is correct |
47 |
Correct |
14 ms |
7128 KB |
Output is correct |
48 |
Correct |
10 ms |
6492 KB |
Output is correct |
49 |
Correct |
5 ms |
6492 KB |
Output is correct |
50 |
Correct |
5 ms |
6232 KB |
Output is correct |
51 |
Correct |
17 ms |
7852 KB |
Output is correct |
52 |
Correct |
17 ms |
7384 KB |
Output is correct |
53 |
Correct |
9 ms |
7004 KB |
Output is correct |
54 |
Correct |
4 ms |
6180 KB |
Output is correct |
55 |
Correct |
4 ms |
5976 KB |
Output is correct |
56 |
Correct |
620 ms |
7392 KB |
Output is correct |
57 |
Correct |
4 ms |
6616 KB |
Output is correct |
58 |
Correct |
644 ms |
6236 KB |
Output is correct |
59 |
Correct |
7 ms |
6488 KB |
Output is correct |
60 |
Correct |
11 ms |
6488 KB |
Output is correct |
61 |
Correct |
8 ms |
6492 KB |
Output is correct |
62 |
Correct |
9 ms |
7004 KB |
Output is correct |
63 |
Correct |
29 ms |
7388 KB |
Output is correct |
64 |
Correct |
22 ms |
8400 KB |
Output is correct |
65 |
Correct |
37 ms |
8408 KB |
Output is correct |
66 |
Correct |
51 ms |
8308 KB |
Output is correct |
67 |
Correct |
29 ms |
8400 KB |
Output is correct |