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>
#define ll int
#define ff first
#define ss second
using namespace std;
ll n,m,i,j,b,p,x,y,R;
vector<ll> v[30005];
set<pair<ll,ll>> q;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(i=0;i<m;i++){
cin >> b >> p;
v[b].push_back(p);
if(i==0) q.insert({0,b});
if(i==1) R=b;
}
vector<ll> dist(n,1e9);
dist[0]=0;
while(!q.empty()){
pair<ll,ll> z=*q.begin();
x=z.ss;
y=z.ff;
q.erase(q.begin());
for(ll i:v[x]){
p=0;
for(b=x;b<n;b+=i){
if(dist[b]>y+p){
if(dist[b]<1e9) q.erase({dist[b],b});
dist[b]=y+p;
q.insert({y+p,b});
}
p++;
}
p=0;
for(b=x;b>=0;b-=i){
if(dist[b]>y+p){
if(dist[b]<1e9) q.erase({dist[b],b});
dist[b]=y+p;
q.insert({y+p,b});
}
p++;
}
}
}
if(dist[R]!=1e9) cout << dist[R];
else cout << -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... |