#include <bits/stdc++.h>
//#include "game.h"
using namespace std;
vector<int> ind[2001];
int len[30001];
int n,m;
int nah;
long long d(int b){
long long cost[m][n];
for(int i = 0;i<m;i++)for(int j = 0;j<n;j++)cost[i][j] = 1e18;
priority_queue<pair<long long,pair<int,int>>> q1;
q1.push({0,{0,b}});
cost[0][b] = 0;
while(!q1.empty()){
long long co = q1.top().first*-1;
long long dog = q1.top().second.first;
long long 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;
int z;
for(int i = 0;i<m;i++){
int 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";
cout<<d(z);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:54:14: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
54 | cout<<d(z);
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |