#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ff first
#define ss second
priority_queue<pii, vector<pii>, greater<pii> > pq;
int dst[30000];
vector<int> edge[30000];
int main(){
ios_base::sync_with_stdio(0), cin.tie(0);
int N, M, st, ed; cin>>N>>M;
for(int i=0; i<M; ++i){
int b,p; cin>>b>>p;
edge[b].push_back(p);
if(!i) st = b;
else if(i==1) ed = b;
}
fill(dst, dst+N, 1e9);
pq.emplace(0, st); dst[st] = 0;
while(pq.size()){
auto x = pq.top(); pq.pop();
if(x.ff != dst[x.ss]) continue;
for(auto p: edge[x.ss]){
for(int j=0; x.ss+p*j<N; ++j){
if(dst[x.ss+p*j] > x.ff + j){
dst[x.ss+p*j] = x.ff + j;
pq.emplace(x.ff + j, x.ss+p*j);
}
}
for(int j=1; x.ss-p*j>=0; ++j){
if(dst[x.ss-p*j] > x.ff + j){
dst[x.ss-p*j] = x.ff + j;
pq.emplace(x.ff + j, x.ss-p*j);
}
}
}
}
cout<<(dst[ed]==1e9?-1:dst[ed]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:21:29: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.emplace(0, st); dst[st] = 0;
~~~~~~~~^~~
skyscraper.cpp:41:15: warning: 'ed' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<(dst[ed]==1e9?-1:dst[ed]);
~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
2 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1024 KB |
Output is correct |
13 |
Correct |
4 ms |
1024 KB |
Output is correct |
14 |
Correct |
4 ms |
1024 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1024 KB |
Output is correct |
13 |
Correct |
4 ms |
1152 KB |
Output is correct |
14 |
Correct |
4 ms |
1024 KB |
Output is correct |
15 |
Correct |
4 ms |
1024 KB |
Output is correct |
16 |
Correct |
3 ms |
1024 KB |
Output is correct |
17 |
Correct |
4 ms |
1152 KB |
Output is correct |
18 |
Correct |
3 ms |
1152 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
6 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1024 KB |
Output is correct |
22 |
Correct |
3 ms |
1024 KB |
Output is correct |
23 |
Correct |
4 ms |
1152 KB |
Output is correct |
24 |
Correct |
4 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1152 KB |
Output is correct |
26 |
Correct |
5 ms |
1152 KB |
Output is correct |
27 |
Correct |
6 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
5 ms |
1280 KB |
Output is correct |
30 |
Correct |
3 ms |
1152 KB |
Output is correct |
31 |
Correct |
4 ms |
1152 KB |
Output is correct |
32 |
Correct |
5 ms |
1152 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
6 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
4 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1152 KB |
Output is correct |
13 |
Correct |
4 ms |
1024 KB |
Output is correct |
14 |
Correct |
4 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1024 KB |
Output is correct |
17 |
Correct |
4 ms |
1152 KB |
Output is correct |
18 |
Correct |
3 ms |
1024 KB |
Output is correct |
19 |
Correct |
3 ms |
1024 KB |
Output is correct |
20 |
Correct |
6 ms |
1152 KB |
Output is correct |
21 |
Correct |
3 ms |
1024 KB |
Output is correct |
22 |
Correct |
3 ms |
1024 KB |
Output is correct |
23 |
Correct |
3 ms |
1160 KB |
Output is correct |
24 |
Correct |
4 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1152 KB |
Output is correct |
26 |
Correct |
6 ms |
1152 KB |
Output is correct |
27 |
Correct |
6 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
5 ms |
1280 KB |
Output is correct |
30 |
Correct |
4 ms |
1152 KB |
Output is correct |
31 |
Correct |
4 ms |
1152 KB |
Output is correct |
32 |
Correct |
4 ms |
1152 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
6 ms |
1280 KB |
Output is correct |
35 |
Correct |
10 ms |
1408 KB |
Output is correct |
36 |
Correct |
4 ms |
1152 KB |
Output is correct |
37 |
Correct |
9 ms |
1280 KB |
Output is correct |
38 |
Correct |
13 ms |
1408 KB |
Output is correct |
39 |
Correct |
14 ms |
1408 KB |
Output is correct |
40 |
Correct |
11 ms |
1408 KB |
Output is correct |
41 |
Correct |
11 ms |
1408 KB |
Output is correct |
42 |
Correct |
62 ms |
1280 KB |
Output is correct |
43 |
Correct |
41 ms |
1400 KB |
Output is correct |
44 |
Correct |
54 ms |
1340 KB |
Output is correct |
45 |
Correct |
9 ms |
1408 KB |
Output is correct |
46 |
Correct |
9 ms |
1408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
2 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
996 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1072 KB |
Output is correct |
10 |
Correct |
2 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
3 ms |
1024 KB |
Output is correct |
13 |
Correct |
4 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1024 KB |
Output is correct |
16 |
Correct |
3 ms |
1024 KB |
Output is correct |
17 |
Correct |
4 ms |
1152 KB |
Output is correct |
18 |
Correct |
2 ms |
1024 KB |
Output is correct |
19 |
Correct |
2 ms |
1024 KB |
Output is correct |
20 |
Correct |
6 ms |
1152 KB |
Output is correct |
21 |
Correct |
2 ms |
1024 KB |
Output is correct |
22 |
Correct |
3 ms |
1024 KB |
Output is correct |
23 |
Correct |
3 ms |
1024 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
4 ms |
1124 KB |
Output is correct |
26 |
Correct |
6 ms |
1136 KB |
Output is correct |
27 |
Correct |
6 ms |
1024 KB |
Output is correct |
28 |
Correct |
4 ms |
1152 KB |
Output is correct |
29 |
Correct |
4 ms |
1280 KB |
Output is correct |
30 |
Correct |
5 ms |
1152 KB |
Output is correct |
31 |
Correct |
3 ms |
1180 KB |
Output is correct |
32 |
Correct |
4 ms |
1176 KB |
Output is correct |
33 |
Correct |
5 ms |
1280 KB |
Output is correct |
34 |
Correct |
6 ms |
1280 KB |
Output is correct |
35 |
Correct |
9 ms |
1408 KB |
Output is correct |
36 |
Correct |
3 ms |
1128 KB |
Output is correct |
37 |
Correct |
7 ms |
1280 KB |
Output is correct |
38 |
Correct |
15 ms |
1332 KB |
Output is correct |
39 |
Correct |
10 ms |
1400 KB |
Output is correct |
40 |
Correct |
12 ms |
1564 KB |
Output is correct |
41 |
Correct |
15 ms |
1428 KB |
Output is correct |
42 |
Correct |
48 ms |
1348 KB |
Output is correct |
43 |
Correct |
47 ms |
1280 KB |
Output is correct |
44 |
Correct |
45 ms |
1344 KB |
Output is correct |
45 |
Correct |
14 ms |
1408 KB |
Output is correct |
46 |
Correct |
9 ms |
1408 KB |
Output is correct |
47 |
Correct |
19 ms |
2148 KB |
Output is correct |
48 |
Correct |
9 ms |
1664 KB |
Output is correct |
49 |
Correct |
8 ms |
1536 KB |
Output is correct |
50 |
Correct |
7 ms |
1568 KB |
Output is correct |
51 |
Correct |
33 ms |
2928 KB |
Output is correct |
52 |
Correct |
40 ms |
2428 KB |
Output is correct |
53 |
Correct |
21 ms |
2428 KB |
Output is correct |
54 |
Correct |
4 ms |
1408 KB |
Output is correct |
55 |
Correct |
4 ms |
1408 KB |
Output is correct |
56 |
Correct |
686 ms |
2556 KB |
Output is correct |
57 |
Correct |
57 ms |
3588 KB |
Output is correct |
58 |
Correct |
647 ms |
1536 KB |
Output is correct |
59 |
Correct |
579 ms |
1592 KB |
Output is correct |
60 |
Correct |
630 ms |
1592 KB |
Output is correct |
61 |
Correct |
471 ms |
1596 KB |
Output is correct |
62 |
Correct |
13 ms |
2048 KB |
Output is correct |
63 |
Correct |
33 ms |
2552 KB |
Output is correct |
64 |
Correct |
39 ms |
3620 KB |
Output is correct |
65 |
Correct |
59 ms |
3564 KB |
Output is correct |
66 |
Correct |
70 ms |
3572 KB |
Output is correct |
67 |
Correct |
63 ms |
3572 KB |
Output is correct |