#include <bits/stdc++.h>
using namespace std;
inline namespace
{
} // namespace
void solve()
{
int n, m;
cin >> n >> m;
int s, t;
vector<vector<int>> doges(n);
for (int i = 0; i < m; i++)
{
int b, p;
cin >> b >> p;
if (i == 0)
s = b;
else if (i == 1)
t = b;
doges[b].push_back(p);
}
for (auto &jumps : doges)
{
sort(jumps.begin(), jumps.end());
jumps.erase(unique(jumps.begin(), jumps.end()), jumps.end());
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n; i++)
{
for (int x : doges[i])
{
for (int c = 1; i + c * x < n; c++)
{
int j = i + c * x;
adj[i].emplace_back(j, c);
if (binary_search(doges[j].begin(), doges[j].end(), x))
break;
}
for (int c = 1; i - c * x >= 0; c++)
{
int j = i - c * x;
adj[i].emplace_back(j, c);
if (binary_search(doges[j].begin(), doges[j].end(), x))
break;
}
}
}
vector<int> dist(n, INT_MAX);
priority_queue<pair<int, int>> pq;
for (dist[s] = 0, pq.emplace(-dist[s], s); !pq.empty();)
{
auto [d, u] = pq.top();
pq.pop();
if (-d != dist[u])
continue;
for (auto [v, x] : adj[u])
{
if (dist[u] + x < dist[v])
dist[v] = dist[u] + x, pq.emplace(-dist[v], v);
}
}
cout << (dist[t] != INT_MAX ? dist[t] : -1) << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:75:20: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | cout << (dist[t] != INT_MAX ? dist[t] : -1) << '\n';
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
452 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
1748 KB |
Output is correct |
34 |
Correct |
4 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
456 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
2 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
3 ms |
1736 KB |
Output is correct |
34 |
Correct |
4 ms |
1752 KB |
Output is correct |
35 |
Correct |
10 ms |
1840 KB |
Output is correct |
36 |
Correct |
3 ms |
656 KB |
Output is correct |
37 |
Correct |
12 ms |
2688 KB |
Output is correct |
38 |
Correct |
15 ms |
2648 KB |
Output is correct |
39 |
Correct |
13 ms |
2656 KB |
Output is correct |
40 |
Correct |
13 ms |
2624 KB |
Output is correct |
41 |
Correct |
13 ms |
2636 KB |
Output is correct |
42 |
Correct |
5 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
832 KB |
Output is correct |
44 |
Correct |
5 ms |
852 KB |
Output is correct |
45 |
Correct |
13 ms |
5204 KB |
Output is correct |
46 |
Correct |
13 ms |
5092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
452 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
724 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
4 ms |
1748 KB |
Output is correct |
34 |
Correct |
4 ms |
1732 KB |
Output is correct |
35 |
Correct |
9 ms |
1748 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
10 ms |
2764 KB |
Output is correct |
38 |
Correct |
13 ms |
2628 KB |
Output is correct |
39 |
Correct |
12 ms |
2676 KB |
Output is correct |
40 |
Correct |
15 ms |
2652 KB |
Output is correct |
41 |
Correct |
15 ms |
2636 KB |
Output is correct |
42 |
Correct |
5 ms |
812 KB |
Output is correct |
43 |
Correct |
4 ms |
852 KB |
Output is correct |
44 |
Correct |
5 ms |
848 KB |
Output is correct |
45 |
Correct |
12 ms |
5200 KB |
Output is correct |
46 |
Correct |
13 ms |
5076 KB |
Output is correct |
47 |
Correct |
38 ms |
10668 KB |
Output is correct |
48 |
Correct |
13 ms |
4540 KB |
Output is correct |
49 |
Correct |
11 ms |
3796 KB |
Output is correct |
50 |
Correct |
9 ms |
3668 KB |
Output is correct |
51 |
Correct |
32 ms |
6608 KB |
Output is correct |
52 |
Correct |
33 ms |
7156 KB |
Output is correct |
53 |
Correct |
15 ms |
4260 KB |
Output is correct |
54 |
Correct |
3 ms |
2260 KB |
Output is correct |
55 |
Correct |
3 ms |
2768 KB |
Output is correct |
56 |
Correct |
11 ms |
3928 KB |
Output is correct |
57 |
Correct |
29 ms |
10836 KB |
Output is correct |
58 |
Correct |
8 ms |
3020 KB |
Output is correct |
59 |
Correct |
9 ms |
3540 KB |
Output is correct |
60 |
Correct |
9 ms |
4180 KB |
Output is correct |
61 |
Correct |
9 ms |
3408 KB |
Output is correct |
62 |
Correct |
17 ms |
8204 KB |
Output is correct |
63 |
Correct |
44 ms |
23712 KB |
Output is correct |
64 |
Correct |
50 ms |
26928 KB |
Output is correct |
65 |
Correct |
66 ms |
36556 KB |
Output is correct |
66 |
Correct |
105 ms |
63600 KB |
Output is correct |
67 |
Correct |
120 ms |
63596 KB |
Output is correct |