#include <bits/stdc++.h>
//#include "game.h"
using namespace std;
vector<short> ind[2001];
short len[30001];
short n,m;
short nah;
short d(short b){
short cost[m][n];
for(int i = 0;i<m;i++)for(int j = 0;j<n;j++)cost[i][j] = 1e5;
priority_queue<pair<short,pair<short,short>>> q1;
q1.push({0,{0,b}});
cost[0][b] = 0;
while(!q1.empty()){
short co = q1.top().first*-1;
short dog = q1.top().second.first;
short indx = q1.top().second.second;
q1.pop();
if(co>cost[dog][indx]) continue ;
for(auto i:ind[indx]){
if(cost[i][indx]>(cost[dog][indx])){
cost[i][indx] = (cost[dog][indx]);
q1.push({cost[i][indx]*-1,{i,indx}});
}
}
if(indx+len[dog]>=0&&indx+len[dog]<n){
if(cost[dog][indx+len[dog]]>(cost[dog][indx]+1)){
cost[dog][indx+len[dog]] = (cost[dog][indx]+1);
q1.push({cost[dog][indx+len[dog]]*-1,{dog,indx+len[dog]}});
}
}
if(indx-len[dog]>=0&&indx-len[dog]<n){
if(cost[dog][indx-len[dog]]>(cost[dog][indx]+1)){
cost[dog][indx-len[dog]] = (cost[dog][indx]+1);
q1.push({cost[dog][indx-len[dog]]*-1,{dog,indx-len[dog]}});
}
}
}
return cost[1][nah];
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
short z;
for(int i = 0;i<m;i++){
short x,b;
cin>>x>>b;
if(i==1)nah = x;
if(i==0)z = x;
len[i] = b;
ind[x].push_back(i);
}
//cout<<z<<" "<<nah<<"\n";
short val = d(z);
if(val==1e5)cout<<-1<<"\n";
else cout<<val<<"\n";
}
Compilation message
skyscraper.cpp: In function 'short int d(short int)':
skyscraper.cpp:10:62: warning: overflow in conversion from 'double' to 'short int' changes value from '1.0e+5' to '32767' [-Woverflow]
10 | for(int i = 0;i<m;i++)for(int j = 0;j<n;j++)cost[i][j] = 1e5;
| ^~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:54:18: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
54 | short val = d(z);
| ~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |