#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 = 0;
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 |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 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 |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
4 ms |
2512 KB |
Output is correct |
13 |
Correct |
4 ms |
2520 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
220 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
4 ms |
2512 KB |
Output is correct |
13 |
Correct |
4 ms |
2520 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 |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
61 ms |
32488 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 |
468 KB |
Output is correct |
26 |
Correct |
62 ms |
33440 KB |
Output is correct |
27 |
Correct |
65 ms |
33472 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
1364 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 |
5 ms |
1968 KB |
Output is correct |
34 |
Correct |
4 ms |
2004 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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
252 KB |
Output is correct |
9 |
Correct |
0 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 |
4 ms |
2512 KB |
Output is correct |
13 |
Correct |
4 ms |
2520 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 |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
64 ms |
32508 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 |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
66 ms |
33440 KB |
Output is correct |
27 |
Correct |
63 ms |
33452 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
4 ms |
1364 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 |
5 ms |
2004 KB |
Output is correct |
34 |
Correct |
4 ms |
2004 KB |
Output is correct |
35 |
Correct |
7 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
496 KB |
Output is correct |
37 |
Correct |
10 ms |
3700 KB |
Output is correct |
38 |
Correct |
11 ms |
3084 KB |
Output is correct |
39 |
Correct |
11 ms |
3156 KB |
Output is correct |
40 |
Correct |
11 ms |
3092 KB |
Output is correct |
41 |
Correct |
15 ms |
2936 KB |
Output is correct |
42 |
Runtime error |
332 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 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 |
4 ms |
2512 KB |
Output is correct |
13 |
Correct |
4 ms |
2520 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 |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
61 ms |
32528 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 |
468 KB |
Output is correct |
26 |
Correct |
75 ms |
33444 KB |
Output is correct |
27 |
Correct |
60 ms |
33444 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 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 |
4 ms |
2004 KB |
Output is correct |
34 |
Correct |
5 ms |
2004 KB |
Output is correct |
35 |
Correct |
9 ms |
2004 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
10 ms |
3796 KB |
Output is correct |
38 |
Correct |
14 ms |
3028 KB |
Output is correct |
39 |
Correct |
14 ms |
3236 KB |
Output is correct |
40 |
Correct |
10 ms |
3156 KB |
Output is correct |
41 |
Correct |
10 ms |
3020 KB |
Output is correct |
42 |
Runtime error |
318 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |