#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()
template<class T> inline bool chmin(T &a, T b){
if(a>b){a=b;return true;}
return false;
}
template<class T> inline bool chmax(T &a, T b){
if(a<b){a=b;return true;}
return false;
}
constexpr int mod = 998244353;
constexpr int inf = 3e18;
int N,M;
vector<int>v[30005];
int dist[30005];
signed main(){
cin>>N>>M;
int start,goal;
rep(i,M){
int a,b;cin>>a>>b;
v[a].push_back(b);
if(!i)start=a;
if(i==1)goal=a;
}
rep(i,N){
sort(all(v[i]));v[i].erase(unique(all(v[i])),v[i].end());
}
priority_queue<P,vector<P>,greater<P>>que;
fill(dist,dist+N,inf);
dist[start]=0;
que.push({0,start});
while(!que.empty()){
P p=que.top();que.pop();
if(dist[p.second]<p.first)continue;
for(int i:v[p.second]){
for(int j=p.second+i;j<N;j+=i){
if(dist[j]>p.first+(j-p.second)/i){
dist[j]=p.first+(j-p.second)/i;
que.push({dist[j],j});
}
if(lower_bound(all(v[j]),i)!=v[j].end()&&*lower_bound(all(v[j]),i)==i)break;
}
for(int j=p.second-i;j>=0;j-=i){
if(dist[j]>p.first+(p.second-j)/i){
dist[j]=p.first+(p.second-j)/i;
que.push({dist[j],j});
}
if(lower_bound(all(v[j]),i)!=v[j].end()&&*lower_bound(all(v[j]),i)==i)break;
}
}
}
cout<<(dist[goal]!=inf?dist[goal]:-1)<<endl;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:61:18: warning: 'goal' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<(dist[goal]!=inf?dist[goal]:-1)<<endl;
~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
3 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1024 KB |
Output is correct |
15 |
Correct |
3 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
2 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1024 KB |
Output is correct |
15 |
Correct |
2 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
2 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
2 ms |
1152 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
2 ms |
1152 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
3 ms |
1152 KB |
Output is correct |
26 |
Correct |
3 ms |
1152 KB |
Output is correct |
27 |
Correct |
2 ms |
1152 KB |
Output is correct |
28 |
Correct |
4 ms |
1152 KB |
Output is correct |
29 |
Correct |
4 ms |
1408 KB |
Output is correct |
30 |
Correct |
3 ms |
1280 KB |
Output is correct |
31 |
Correct |
3 ms |
1280 KB |
Output is correct |
32 |
Correct |
2 ms |
1280 KB |
Output is correct |
33 |
Correct |
7 ms |
1660 KB |
Output is correct |
34 |
Correct |
6 ms |
1788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
2 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1024 KB |
Output is correct |
15 |
Correct |
2 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
2 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
2 ms |
1152 KB |
Output is correct |
21 |
Correct |
2 ms |
1024 KB |
Output is correct |
22 |
Correct |
2 ms |
1024 KB |
Output is correct |
23 |
Correct |
2 ms |
1152 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
3 ms |
1152 KB |
Output is correct |
26 |
Correct |
3 ms |
1152 KB |
Output is correct |
27 |
Correct |
3 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
4 ms |
1408 KB |
Output is correct |
30 |
Correct |
2 ms |
1280 KB |
Output is correct |
31 |
Correct |
3 ms |
1280 KB |
Output is correct |
32 |
Correct |
2 ms |
1280 KB |
Output is correct |
33 |
Correct |
6 ms |
1660 KB |
Output is correct |
34 |
Correct |
6 ms |
1788 KB |
Output is correct |
35 |
Correct |
23 ms |
1664 KB |
Output is correct |
36 |
Correct |
5 ms |
1152 KB |
Output is correct |
37 |
Correct |
20 ms |
1664 KB |
Output is correct |
38 |
Correct |
30 ms |
1920 KB |
Output is correct |
39 |
Correct |
30 ms |
1912 KB |
Output is correct |
40 |
Correct |
29 ms |
1912 KB |
Output is correct |
41 |
Correct |
29 ms |
1912 KB |
Output is correct |
42 |
Correct |
20 ms |
1664 KB |
Output is correct |
43 |
Correct |
20 ms |
1664 KB |
Output is correct |
44 |
Correct |
20 ms |
1620 KB |
Output is correct |
45 |
Correct |
32 ms |
2300 KB |
Output is correct |
46 |
Correct |
26 ms |
2300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
3 ms |
1024 KB |
Output is correct |
12 |
Correct |
2 ms |
1024 KB |
Output is correct |
13 |
Correct |
2 ms |
1024 KB |
Output is correct |
14 |
Correct |
2 ms |
1024 KB |
Output is correct |
15 |
Correct |
2 ms |
1024 KB |
Output is correct |
16 |
Correct |
2 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 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 |
3 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 |
2 ms |
1152 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
3 ms |
1152 KB |
Output is correct |
26 |
Correct |
2 ms |
1152 KB |
Output is correct |
27 |
Correct |
3 ms |
1152 KB |
Output is correct |
28 |
Correct |
3 ms |
1152 KB |
Output is correct |
29 |
Correct |
4 ms |
1408 KB |
Output is correct |
30 |
Correct |
2 ms |
1280 KB |
Output is correct |
31 |
Correct |
2 ms |
1280 KB |
Output is correct |
32 |
Correct |
2 ms |
1280 KB |
Output is correct |
33 |
Correct |
6 ms |
1788 KB |
Output is correct |
34 |
Correct |
6 ms |
1788 KB |
Output is correct |
35 |
Correct |
33 ms |
1656 KB |
Output is correct |
36 |
Correct |
5 ms |
1152 KB |
Output is correct |
37 |
Correct |
20 ms |
1664 KB |
Output is correct |
38 |
Correct |
28 ms |
1912 KB |
Output is correct |
39 |
Correct |
28 ms |
1920 KB |
Output is correct |
40 |
Correct |
28 ms |
1920 KB |
Output is correct |
41 |
Correct |
30 ms |
1912 KB |
Output is correct |
42 |
Correct |
19 ms |
1664 KB |
Output is correct |
43 |
Correct |
19 ms |
1664 KB |
Output is correct |
44 |
Correct |
19 ms |
1664 KB |
Output is correct |
45 |
Correct |
25 ms |
2304 KB |
Output is correct |
46 |
Correct |
28 ms |
2308 KB |
Output is correct |
47 |
Correct |
42 ms |
2936 KB |
Output is correct |
48 |
Correct |
20 ms |
1912 KB |
Output is correct |
49 |
Correct |
19 ms |
1920 KB |
Output is correct |
50 |
Correct |
15 ms |
1792 KB |
Output is correct |
51 |
Correct |
49 ms |
3316 KB |
Output is correct |
52 |
Correct |
51 ms |
3316 KB |
Output is correct |
53 |
Correct |
35 ms |
3452 KB |
Output is correct |
54 |
Correct |
2 ms |
1664 KB |
Output is correct |
55 |
Correct |
3 ms |
1664 KB |
Output is correct |
56 |
Correct |
27 ms |
2464 KB |
Output is correct |
57 |
Correct |
29 ms |
2556 KB |
Output is correct |
58 |
Correct |
16 ms |
2044 KB |
Output is correct |
59 |
Correct |
24 ms |
2164 KB |
Output is correct |
60 |
Correct |
24 ms |
2172 KB |
Output is correct |
61 |
Correct |
24 ms |
2172 KB |
Output is correct |
62 |
Correct |
30 ms |
2560 KB |
Output is correct |
63 |
Correct |
62 ms |
6120 KB |
Output is correct |
64 |
Correct |
65 ms |
6136 KB |
Output is correct |
65 |
Correct |
81 ms |
6120 KB |
Output is correct |
66 |
Correct |
159 ms |
10220 KB |
Output is correct |
67 |
Correct |
154 ms |
10220 KB |
Output is correct |