This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define N 30005
struct A{
int a,b,c;
};
int a[N],p[N];
vector<int> g[N];
map<int,int> mp[N];
queue<A> q;
int main(){
int n,m,i;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++){
scanf("%d %d",&a[i],&p[i]);
g[a[i]].push_back(p[i]);
}
q.push({a[0],p[0],0});
mp[a[0]][p[0]]=0;
while(!q.empty()){
auto [x,po,w]=q.front();
q.pop();
if(x==a[1]){printf("%d",w);return 0;}
if(x>po&&!mp[x-po].count(po))q.push({x-po,po,w+1}),mp[x-po][po]=w+1;
if(x+po<=n&&!mp[x+po].count(po))q.push({x+po,po,w+1}),mp[x+po][po]=w+1;
for(auto po:g[x]){
if(x>po&&!mp[x-po].count(po))q.push({x-po,po,w+1}),mp[x-po][po]=w+1;
if(x+po<=n&&!mp[x+po].count(po))q.push({x+po,po,w+1}),mp[x+po][po]=w+1;
}
}
printf("-1");
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:20:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d %d",&a[i],&p[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |