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;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
int b[m],p[m];
vector<int>adj[n];
for(int i=0;i<m;i++){
cin>>b[i]>>p[i];
adj[b[i]].pb(p[i]);
}
set<pair<int,int>>s;
s.insert({0,b[0]});
vector<int>d(n,1e9);
d[b[0]]=0;
while(!s.empty()){
pair<int,int>q=*s.begin();
s.erase(s.begin());
int C=q.ff,p=q.ss;
for(int i:adj[p]){
int cur=p,cost=0;
while(cur>=0){
if(C+cost<d[cur]){
if(d[cur]<1e9) s.erase({d[cur],cur});
d[cur]=C+cost;
s.insert({d[cur],cur});
}
cur-=i;
cost++;
}
cur=p;
cost=0;
while(cur<n){
if(C+cost<d[cur]){
if(d[cur]<1e9) s.erase({d[cur],cur});
d[cur]=C+cost;
s.insert({d[cur],cur});
}
cur+=i;
cost++;
}
}
}
if(d[b[1]]==1e9){
cout<<"-1";
}
else{
cout<<d[b[1]];
}
}
# | 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... |