#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<array<int, 2>>> adj(n);
// vector<set<array<int, 2>>> adj(n);
vector<set<int>> jump(n);
vector<array<int, 2>> info(m);
int src, dest;
for(int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
info[i] = {b, p};
jump[b].insert(p);
if(i == 0) src = b;
if(i == 1) dest = b;
}
/*
for(int i = 0; i < m; i++){
auto [b, p] = info[i];
for(int j = b+p; j < n; j += p){
adj[b].push_back({j, (j-b)/p});
if(jump[j].count(p)) break;
}
for(int j = b-p; j >= 0; j -= p){
adj[b].push_back({j, (b-j)/p});
if(jump[j].count(p)) break;
}
}*/
for(int b = 0; b < n; b++){
for(int p : jump[b]){
for(int j = b+p; j < n; j += p){
adj[b].push_back({j, (j-b)/p});
if(jump[j].count(p)) break;
}
for(int j = b-p; j >= 0; j -= p){
adj[b].push_back({j, (b-j)/p});
if(jump[j].count(p)) break;
}
}
}
vector<int> dist(n, INF);
dist[src] = 0;
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
pq.push({0, src});
while(!pq.empty()){
auto [d, node] = pq.top();
pq.pop();
for(auto& [v, nd] : adj[node]){
if(dist[v] <= dist[node] + nd)
continue;
dist[v] = dist[node] + nd;
pq.push({dist[v], v});
}
}
cout << (dist[dest] == INF ? -1 : dist[dest]) << '\n';
return 0;
}
Compilation message
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:67:20: warning: 'dest' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | cout << (dist[dest] == INF ? -1 : dist[dest]) << '\n';
| ^
skyscraper.cpp:53:10: warning: 'src' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | dist[src] = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
268 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
41 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
85 ms |
876 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
87 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
876 KB |
Output is correct |
29 |
Correct |
4 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
2 ms |
1004 KB |
Output is correct |
32 |
Correct |
2 ms |
876 KB |
Output is correct |
33 |
Correct |
7 ms |
2028 KB |
Output is correct |
34 |
Correct |
7 ms |
2028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
748 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
90 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
2 ms |
748 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
876 KB |
Output is correct |
29 |
Correct |
4 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
3 ms |
1004 KB |
Output is correct |
32 |
Correct |
2 ms |
876 KB |
Output is correct |
33 |
Correct |
7 ms |
2028 KB |
Output is correct |
34 |
Correct |
7 ms |
2028 KB |
Output is correct |
35 |
Correct |
18 ms |
3052 KB |
Output is correct |
36 |
Correct |
3 ms |
876 KB |
Output is correct |
37 |
Correct |
21 ms |
3692 KB |
Output is correct |
38 |
Correct |
24 ms |
4204 KB |
Output is correct |
39 |
Correct |
24 ms |
4204 KB |
Output is correct |
40 |
Correct |
24 ms |
4204 KB |
Output is correct |
41 |
Correct |
24 ms |
4224 KB |
Output is correct |
42 |
Correct |
6 ms |
1004 KB |
Output is correct |
43 |
Correct |
6 ms |
1004 KB |
Output is correct |
44 |
Correct |
6 ms |
1132 KB |
Output is correct |
45 |
Correct |
30 ms |
6892 KB |
Output is correct |
46 |
Correct |
30 ms |
6892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
748 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
748 KB |
Output is correct |
25 |
Correct |
2 ms |
748 KB |
Output is correct |
26 |
Correct |
1 ms |
620 KB |
Output is correct |
27 |
Correct |
1 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
876 KB |
Output is correct |
29 |
Correct |
4 ms |
1388 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
2 ms |
1004 KB |
Output is correct |
32 |
Correct |
2 ms |
876 KB |
Output is correct |
33 |
Correct |
7 ms |
2028 KB |
Output is correct |
34 |
Correct |
8 ms |
2028 KB |
Output is correct |
35 |
Correct |
19 ms |
3052 KB |
Output is correct |
36 |
Correct |
4 ms |
876 KB |
Output is correct |
37 |
Correct |
21 ms |
3692 KB |
Output is correct |
38 |
Correct |
25 ms |
4076 KB |
Output is correct |
39 |
Correct |
28 ms |
4076 KB |
Output is correct |
40 |
Correct |
34 ms |
4076 KB |
Output is correct |
41 |
Correct |
25 ms |
4076 KB |
Output is correct |
42 |
Correct |
6 ms |
1004 KB |
Output is correct |
43 |
Correct |
6 ms |
1004 KB |
Output is correct |
44 |
Correct |
6 ms |
1132 KB |
Output is correct |
45 |
Correct |
31 ms |
6892 KB |
Output is correct |
46 |
Correct |
35 ms |
6872 KB |
Output is correct |
47 |
Correct |
62 ms |
12008 KB |
Output is correct |
48 |
Correct |
20 ms |
5864 KB |
Output is correct |
49 |
Correct |
17 ms |
5100 KB |
Output is correct |
50 |
Correct |
14 ms |
4844 KB |
Output is correct |
51 |
Correct |
50 ms |
8568 KB |
Output is correct |
52 |
Correct |
55 ms |
8976 KB |
Output is correct |
53 |
Correct |
25 ms |
6000 KB |
Output is correct |
54 |
Correct |
4 ms |
3052 KB |
Output is correct |
55 |
Correct |
6 ms |
3432 KB |
Output is correct |
56 |
Correct |
17 ms |
5356 KB |
Output is correct |
57 |
Correct |
47 ms |
11624 KB |
Output is correct |
58 |
Correct |
11 ms |
3940 KB |
Output is correct |
59 |
Correct |
12 ms |
4328 KB |
Output is correct |
60 |
Correct |
14 ms |
4968 KB |
Output is correct |
61 |
Correct |
14 ms |
4460 KB |
Output is correct |
62 |
Correct |
32 ms |
9704 KB |
Output is correct |
63 |
Correct |
101 ms |
26980 KB |
Output is correct |
64 |
Correct |
100 ms |
30048 KB |
Output is correct |
65 |
Correct |
132 ms |
39776 KB |
Output is correct |
66 |
Correct |
285 ms |
68056 KB |
Output is correct |
67 |
Correct |
295 ms |
68056 KB |
Output is correct |