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;
vector<int>g[30000],b(30000),p(30000);
int main(){
int n,m;cin>>n>>m;
for(int i=0;i<m;i++){
cin>>b[i]>>p[i];
g[b[i]].push_back(p[i]);
}
queue<pair<int,int>>q;
q.push({0,b[0]});
vector<int>dist(n+5,1e9);
dist[b[0]]=0;
map<pair<int,int>,bool>v;
while(q.size()){
pair<int,int>x=q.front();
q.pop();
dist[x.second]=min(dist[x.second],x.first);
for(auto i:g[x.second]){
int u=x.second+i;
while(u<n){
if(!v[{x.second,u}]){
g[u].push_back(i);
v[{x.second,u}]=1;
//cout<<x.second<<" "<<u<<" "<<x.first+1<<endl;
q.push({x.first+1,u});
}
break;
}
u=x.second-i;
while(u>=0){
if(!v[{x.second,u}]){
g[u].push_back(i);
v[{x.second,u}]=1;
//cout<<x.second<<" "<<u<<" "<<x.first+1<<endl;
q.push({x.first+1,u});
}
break;
}
}
}
cout<<(dist[b[1]]==1e9?-1:dist[b[1]])<<endl;
}
# | 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... |