#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define sint long long
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
sint n,m,t,tt,s,e;cin>>n>>m;
int dist[n+5],minadd[n+5];
vector<sint>conn[n+5];
set<sint>con2[n+5];
memset(dist,-1,sizeof dist);
memset(minadd,-1,sizeof minadd);
int mtt=0;
for(int i=0;i<m;i++){
cin>>t>>tt;
mtt=max(mtt,tt);
if(i==0)s=t;
if(i==1)e=t;
if(tt!=0)con2[t].insert(tt);
}
if(mtt==1){
cout<<abs(s-e)<<'\n';
return 0;
}
for(int i=0;i<n;i++){
for(auto&x:con2[i])conn[i].pb(x);
}
priority_queue<pair<int,sint>,vector<pair<int,sint>>,greater<>>dij;
dij.push(mp(0,s));
while(!dij.empty()){
sint now=dij.top().se;
if(dist[now]!=-1){dij.pop();continue;}
dist[now]=dij.top().fi;
dij.pop();
for(auto x:conn[now]){
//cout<<x;
int nnow=now-now/x*x;
while(nnow<n){
if(now==nnow){nnow+=x;continue;}
if(dist[nnow]==-1){
int di=dist[now]+abs(now-nnow)/x;
//cout<<x<<dist[now]+abs(i)<<" "<<minadd[now+i*x];
if(di<minadd[nnow]||minadd[nnow]==-1){
dij.push(mp(di,nnow));
minadd[nnow]=di;
}
}
nnow+=x;
}
}
}
//for(int i=0;i<n;i++)cout<<dist[i]<<" ";
cout<<dist[e]<<"\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:59:17: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | cout<<dist[e]<<"\n";
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 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 |
2 ms |
512 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 |
3 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 |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
748 KB |
Output is correct |
25 |
Correct |
3 ms |
748 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
4 ms |
1004 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
3 ms |
748 KB |
Output is correct |
32 |
Correct |
2 ms |
748 KB |
Output is correct |
33 |
Correct |
6 ms |
1260 KB |
Output is correct |
34 |
Correct |
7 ms |
1260 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 |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
400 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
620 KB |
Output is correct |
16 |
Correct |
2 ms |
364 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 |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
748 KB |
Output is correct |
25 |
Correct |
3 ms |
748 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
4 ms |
1004 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
3 ms |
748 KB |
Output is correct |
32 |
Correct |
2 ms |
748 KB |
Output is correct |
33 |
Correct |
6 ms |
1260 KB |
Output is correct |
34 |
Correct |
6 ms |
1260 KB |
Output is correct |
35 |
Correct |
14 ms |
2284 KB |
Output is correct |
36 |
Correct |
3 ms |
748 KB |
Output is correct |
37 |
Correct |
11 ms |
2028 KB |
Output is correct |
38 |
Correct |
15 ms |
2796 KB |
Output is correct |
39 |
Correct |
16 ms |
2796 KB |
Output is correct |
40 |
Correct |
16 ms |
2796 KB |
Output is correct |
41 |
Correct |
20 ms |
2796 KB |
Output is correct |
42 |
Correct |
7 ms |
748 KB |
Output is correct |
43 |
Correct |
6 ms |
748 KB |
Output is correct |
44 |
Correct |
6 ms |
748 KB |
Output is correct |
45 |
Correct |
21 ms |
3076 KB |
Output is correct |
46 |
Correct |
21 ms |
3056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
3 ms |
748 KB |
Output is correct |
25 |
Correct |
3 ms |
748 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
620 KB |
Output is correct |
29 |
Correct |
4 ms |
1004 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
31 |
Correct |
3 ms |
748 KB |
Output is correct |
32 |
Correct |
3 ms |
748 KB |
Output is correct |
33 |
Correct |
6 ms |
1260 KB |
Output is correct |
34 |
Correct |
7 ms |
1260 KB |
Output is correct |
35 |
Correct |
13 ms |
2284 KB |
Output is correct |
36 |
Correct |
4 ms |
748 KB |
Output is correct |
37 |
Correct |
11 ms |
2028 KB |
Output is correct |
38 |
Correct |
16 ms |
2796 KB |
Output is correct |
39 |
Correct |
16 ms |
2796 KB |
Output is correct |
40 |
Correct |
17 ms |
2796 KB |
Output is correct |
41 |
Correct |
20 ms |
2796 KB |
Output is correct |
42 |
Correct |
7 ms |
748 KB |
Output is correct |
43 |
Correct |
6 ms |
748 KB |
Output is correct |
44 |
Correct |
6 ms |
748 KB |
Output is correct |
45 |
Correct |
22 ms |
3068 KB |
Output is correct |
46 |
Correct |
24 ms |
3328 KB |
Output is correct |
47 |
Correct |
28 ms |
4452 KB |
Output is correct |
48 |
Correct |
12 ms |
4076 KB |
Output is correct |
49 |
Correct |
11 ms |
4204 KB |
Output is correct |
50 |
Correct |
9 ms |
3948 KB |
Output is correct |
51 |
Correct |
39 ms |
7520 KB |
Output is correct |
52 |
Correct |
39 ms |
6500 KB |
Output is correct |
53 |
Correct |
22 ms |
6500 KB |
Output is correct |
54 |
Correct |
4 ms |
3312 KB |
Output is correct |
55 |
Correct |
5 ms |
3312 KB |
Output is correct |
56 |
Correct |
14 ms |
4716 KB |
Output is correct |
57 |
Correct |
71 ms |
7388 KB |
Output is correct |
58 |
Correct |
12 ms |
3440 KB |
Output is correct |
59 |
Correct |
11 ms |
3712 KB |
Output is correct |
60 |
Correct |
13 ms |
3564 KB |
Output is correct |
61 |
Correct |
15 ms |
3692 KB |
Output is correct |
62 |
Correct |
22 ms |
5612 KB |
Output is correct |
63 |
Correct |
53 ms |
9180 KB |
Output is correct |
64 |
Correct |
68 ms |
9188 KB |
Output is correct |
65 |
Correct |
78 ms |
9180 KB |
Output is correct |
66 |
Correct |
158 ms |
13276 KB |
Output is correct |
67 |
Correct |
152 ms |
13148 KB |
Output is correct |