#include <bits/stdc++.h>
//#include "game.h"
using namespace std;
vector<long long> ind[30001];
long long len[30001];
long long n,m;
long long nah,z;
long long d(int b){
long long cost[n+1];
for(int i = 0;i<=n;i++)cost[i] = 1e18;
priority_queue<pair<long long,long long>> q1;
q1.push({0,z});
cost[z] = 0;
while(!q1.empty()){
long long co = q1.top().first*-1;
long long no = q1.top().second;
//cout<<lin<<" "<<indx<<"\n";
q1.pop();
if(co>cost[no]) continue ;
for(auto i:ind[no]){
int c = 0;
for(int j = no;j<n;j+=len[i]){
if(cost[j]>cost[no]+c){
cost[j] = cost[no]+c;
q1.push({cost[j]*-1,j});
}
c++;
}
c = 1;
for(int j = no-len[i];j>=0;j-=len[i]){
if(cost[j]>cost[no]+c){
cost[j] = cost[no]+c;
q1.push({cost[j]*-1,j});
}
c++;
}
}
}
return cost[nah];
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
long long z;
for(int i = 0;i<m;i++){
long long x,b;
cin>>x>>b;
b = min(b,n);
if(i==1)nah = x;
if(i==0)z = x;
len[i] = b;
ind[x].push_back(i);
}
//cout<<z<<" "<<nah<<"\n";
long long val = d(z);
if(val==1e18)cout<<-1<<"\n";
else cout<<val<<"\n";
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:55:22: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | long long val = d(z);
| ~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1016 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1032 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1032 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1032 KB |
Output is correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1032 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
1032 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |