이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
llo b[2020];
llo p[2020];
vector<pair<llo,llo>> adj[2020][2020];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,m;
cin>>n>>m;
//map<pair<llo,llo><llo,llo>,llo> aa;
for(llo i=0;i<m;i++){
cin>>b[i]>>p[i];
for(llo 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(llo 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<llo,llo>>> aaa;
aaa.push(mp(0,mp(b[0],p[0])));
aaa.push(mp(0,mp(b[0],2010)));
while(!aaa.empty()){
pair<llo,pair<llo,llo>> 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){
/*llo 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... |