#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);
void solve(){
int n , m;
cin >> n >> m;
int rt = 100;
vector<pair<int,int>> adj[n + 1][rt+1];
vector<int> v[n];
int s , e;
for(int i = 0; i < m; i++){
int b , p;
cin >> b >> p;
if(i == 0)s = b;
if(i == 1)e = b;
if(p <= rt)
v[b].pb(p);
if(p > rt){
int pos = b , dis = 0;
while(pos - p >= 0){
pos -= p;
dis++;
adj[b][0].pb(mp(pos , dis));
}
pos = b;
dis = 0;
while(pos + p < n){
pos += p;
dis++;
adj[b][0].pb(mp(pos , dis));
}
}
}
for(int i = 0; i < n; i++){
for(int j = 1; j <= rt; j++){
if(i - j >= 0){
adj[i][j].pb(mp(i-j , 1));
}
if(i + j < n){
adj[i][j].pb(mp(i+j , 1));
}
}
}
int dis[n][rt+1];
for(int i = 0; i < n; i++){
for(int j = 0; j <= rt; j++){
dis[i][j] = 1e9;
}
}
priority_queue< pair<int,pair<int,int>> , vector<pair<int,pair<int,int>>> , greater<pair<int,pair<int,int>>> > pq;
dis[s][0] = 0;
pq.push(mp(0 , mp(s , 0)));
while(pq.size()){
int di = pq.top().vf , pos = pq.top().vs.vf , pow = pq.top().vs.vs;
//cout << di << ' ' << pos << ' ' << pow << '\n';
pq.pop();
//cout << pos << ' ' << pow << endl;
if(dis[pos][pow] < di)continue;
if(pow == 0){
for(int i : v[pos]){
pq.push(mp(di , mp(pos , i)));
}
}
for(auto i : adj[pos][pow]){
if(dis[i.vf][pow] > di + i.vs){
dis[i.vf][pow] = di + i.vs;
if(pow != 0 && dis[i.vf][0] > di + i.vs){
dis[i.vf][0] = di + i.vs;
pq.push(mp(di + i.vs,mp(i.vf , 0)));
}
pq.push(mp(di + i.vs , mp(i.vf , pow)));
}
}
}
cout << (dis[e][0] == 1e9 ? -1 : dis[e][0]) << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:8:30: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
8 | #define mp(a,b) make_pair(a,b)
| ^
skyscraper.cpp:27:6: note: 's' was declared here
27 | int s , e;
| ^
skyscraper.cpp:92:19: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
92 | cout << (dis[e][0] == 1e9 ? -1 : dis[e][0]) << '\n';
| ~~~~~~~~^
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
1492 KB |
Output is correct |
17 |
Correct |
11 ms |
4732 KB |
Output is correct |
18 |
Correct |
19 ms |
10588 KB |
Output is correct |
19 |
Correct |
21 ms |
12156 KB |
Output is correct |
20 |
Correct |
24 ms |
12156 KB |
Output is correct |
21 |
Correct |
4 ms |
2644 KB |
Output is correct |
22 |
Correct |
18 ms |
10880 KB |
Output is correct |
23 |
Correct |
17 ms |
9812 KB |
Output is correct |
24 |
Correct |
22 ms |
11648 KB |
Output is correct |
25 |
Correct |
21 ms |
12088 KB |
Output is correct |
26 |
Correct |
23 ms |
12124 KB |
Output is correct |
27 |
Correct |
27 ms |
12080 KB |
Output is correct |
28 |
Correct |
26 ms |
12116 KB |
Output is correct |
29 |
Correct |
35 ms |
12192 KB |
Output is correct |
30 |
Correct |
26 ms |
12156 KB |
Output is correct |
31 |
Correct |
25 ms |
12116 KB |
Output is correct |
32 |
Correct |
25 ms |
12168 KB |
Output is correct |
33 |
Correct |
37 ms |
12204 KB |
Output is correct |
34 |
Correct |
38 ms |
12276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
852 KB |
Output is correct |
16 |
Correct |
3 ms |
1492 KB |
Output is correct |
17 |
Correct |
10 ms |
4784 KB |
Output is correct |
18 |
Correct |
17 ms |
10668 KB |
Output is correct |
19 |
Correct |
22 ms |
12128 KB |
Output is correct |
20 |
Correct |
22 ms |
12216 KB |
Output is correct |
21 |
Correct |
5 ms |
2644 KB |
Output is correct |
22 |
Correct |
21 ms |
10924 KB |
Output is correct |
23 |
Correct |
19 ms |
9756 KB |
Output is correct |
24 |
Correct |
23 ms |
11580 KB |
Output is correct |
25 |
Correct |
21 ms |
12100 KB |
Output is correct |
26 |
Correct |
21 ms |
12212 KB |
Output is correct |
27 |
Correct |
20 ms |
12116 KB |
Output is correct |
28 |
Correct |
24 ms |
12140 KB |
Output is correct |
29 |
Correct |
31 ms |
12208 KB |
Output is correct |
30 |
Correct |
23 ms |
12116 KB |
Output is correct |
31 |
Correct |
28 ms |
12160 KB |
Output is correct |
32 |
Correct |
27 ms |
12168 KB |
Output is correct |
33 |
Correct |
49 ms |
12256 KB |
Output is correct |
34 |
Correct |
41 ms |
12124 KB |
Output is correct |
35 |
Correct |
29 ms |
9300 KB |
Output is correct |
36 |
Correct |
13 ms |
6740 KB |
Output is correct |
37 |
Correct |
52 ms |
12864 KB |
Output is correct |
38 |
Correct |
47 ms |
13012 KB |
Output is correct |
39 |
Correct |
48 ms |
13080 KB |
Output is correct |
40 |
Correct |
50 ms |
13076 KB |
Output is correct |
41 |
Correct |
52 ms |
12984 KB |
Output is correct |
42 |
Correct |
28 ms |
12820 KB |
Output is correct |
43 |
Correct |
35 ms |
12824 KB |
Output is correct |
44 |
Correct |
32 ms |
12916 KB |
Output is correct |
45 |
Correct |
69 ms |
15212 KB |
Output is correct |
46 |
Correct |
62 ms |
15132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
852 KB |
Output is correct |
16 |
Correct |
2 ms |
1492 KB |
Output is correct |
17 |
Correct |
12 ms |
4792 KB |
Output is correct |
18 |
Correct |
18 ms |
10616 KB |
Output is correct |
19 |
Correct |
21 ms |
12068 KB |
Output is correct |
20 |
Correct |
21 ms |
12112 KB |
Output is correct |
21 |
Correct |
5 ms |
2644 KB |
Output is correct |
22 |
Correct |
20 ms |
10900 KB |
Output is correct |
23 |
Correct |
18 ms |
9704 KB |
Output is correct |
24 |
Correct |
21 ms |
11644 KB |
Output is correct |
25 |
Correct |
21 ms |
12156 KB |
Output is correct |
26 |
Correct |
23 ms |
12136 KB |
Output is correct |
27 |
Correct |
20 ms |
12208 KB |
Output is correct |
28 |
Correct |
23 ms |
12224 KB |
Output is correct |
29 |
Correct |
40 ms |
12116 KB |
Output is correct |
30 |
Correct |
24 ms |
12108 KB |
Output is correct |
31 |
Correct |
33 ms |
12116 KB |
Output is correct |
32 |
Correct |
24 ms |
12104 KB |
Output is correct |
33 |
Correct |
41 ms |
12220 KB |
Output is correct |
34 |
Correct |
39 ms |
12232 KB |
Output is correct |
35 |
Correct |
33 ms |
9264 KB |
Output is correct |
36 |
Correct |
14 ms |
6796 KB |
Output is correct |
37 |
Correct |
49 ms |
12756 KB |
Output is correct |
38 |
Correct |
54 ms |
13040 KB |
Output is correct |
39 |
Correct |
54 ms |
13084 KB |
Output is correct |
40 |
Correct |
64 ms |
12980 KB |
Output is correct |
41 |
Correct |
73 ms |
13056 KB |
Output is correct |
42 |
Correct |
34 ms |
12820 KB |
Output is correct |
43 |
Correct |
27 ms |
12780 KB |
Output is correct |
44 |
Correct |
27 ms |
12820 KB |
Output is correct |
45 |
Correct |
67 ms |
15188 KB |
Output is correct |
46 |
Correct |
81 ms |
15156 KB |
Output is correct |
47 |
Correct |
319 ms |
78380 KB |
Output is correct |
48 |
Correct |
254 ms |
139128 KB |
Output is correct |
49 |
Correct |
275 ms |
151628 KB |
Output is correct |
50 |
Correct |
290 ms |
166664 KB |
Output is correct |
51 |
Correct |
397 ms |
180752 KB |
Output is correct |
52 |
Correct |
431 ms |
180632 KB |
Output is correct |
53 |
Correct |
330 ms |
178368 KB |
Output is correct |
54 |
Correct |
299 ms |
177952 KB |
Output is correct |
55 |
Correct |
302 ms |
177848 KB |
Output is correct |
56 |
Correct |
360 ms |
179040 KB |
Output is correct |
57 |
Correct |
473 ms |
178068 KB |
Output is correct |
58 |
Correct |
349 ms |
178736 KB |
Output is correct |
59 |
Correct |
336 ms |
178836 KB |
Output is correct |
60 |
Correct |
370 ms |
179324 KB |
Output is correct |
61 |
Correct |
312 ms |
179112 KB |
Output is correct |
62 |
Correct |
385 ms |
180940 KB |
Output is correct |
63 |
Correct |
417 ms |
201220 KB |
Output is correct |
64 |
Correct |
418 ms |
206752 KB |
Output is correct |
65 |
Correct |
459 ms |
216248 KB |
Output is correct |
66 |
Correct |
980 ms |
218032 KB |
Output is correct |
67 |
Correct |
959 ms |
218324 KB |
Output is correct |