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 <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
llo dis[2020][2020];
int b[30001];
int p[30001];
/*void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen((name+".in").c_str(),"r",stdin);
// freopen((name+"p.out").c_str(),"w",stdout);
}*/
vector<pair<int,int>> adj[2020][2020];
int main(){
//setIO("doge_4_1");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
llo m;
cin>>n>>m;
//map<pair<int,int><int,int>,int> aa;
for(llo i=0;i<m;i++){
cin>>b[i]>>p[i];
for(int j=b[i]-p[i];j>=0;j=j-p[i]){
adj[j+p[i]][p[i]].pb(mp(j,p[i]));
adj[j+p[i]][p[i]].pb(mp(j,2010));
}
for(int j=b[i]+p[i];j<n;j=j+p[i]){
adj[j-p[i]][p[i]].pb(mp(j,p[i]));
adj[j-p[i]][p[i]].pb(mp(j,2010));
}
adj[b[i]][2010].pb(mp(b[i],p[i]));
adj[b[i]][p[i]].pb(mp(b[i],2010));
}
memset(dis,-1,sizeof(dis));
dis[b[0]][2010]=0;
dis[b[0]][p[0]]=0;
priority_queue<pair<llo,pair<int,int>>> aaa;
aaa.push(mp(0,mp(b[0],p[0])));
aaa.push(mp(0,mp(b[0],2010)));
while(!aaa.empty()){
pair<int,pair<int,int>> x;
x=aaa.top();
// cout<<x.a<<" "<<x.b<<" "<<dis[x.a][x.b]<<endl;
aaa.pop();
x.a=-x.a;
llo co=1;
if(x.b.b==2010){
co=0;
}
for(auto nn:adj[x.b.a][x.b.b]){
if(dis[nn.a][nn.b]==-1 or dis[nn.a][nn.b]>x.a+co){
/*int st=0;
if(nn.b==2010 and x.b!=2010){
co=0;
st=1;
}*/
dis[nn.a][nn.b]=x.a+co;
if(co==0){
aaa.push(mp(-dis[nn.a][nn.b],nn));
}
else{
aaa.push(mp(-dis[nn.a][nn.b],nn));
}
/*if(st){
co=1;
}*/
}
}
}
/* if(b[0]==b[1]){
cout<<0<<endl;
return 0;
}*/
cout<<dis[b[1]][p[1]]<<endl;
return 0;
}
# | 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... |