이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,m;
cin>>n>>m;
llo b[m];
llo p[m];
//map<pair<llo,llo>,llo> aa;
vector<pair<llo,llo>> adj[n];
for(llo i=0;i<m;i++){
cin>>b[i]>>p[i];
llo jj=1;
for(llo j=b[i]-p[i];j>=0;j=j-p[i]){
adj[b[i]].pb(mp(j,jj));
jj+=1;
}
jj=1;
for(llo j=b[i]+p[i];j<n;j=j+p[i]){
adj[b[i]].pb(mp(j,jj));
jj+=1;
}
}
if(n==1){
cout<<-1<<endl;
return 0;
}
llo dis[n];
for(llo i=0;i<n;i++){
dis[i]=-1;
}
dis[b[0]]=0;
priority_queue<pair<llo,llo>> aaa;
aaa.push(mp(0,b[0]));
while(aaa.size()>0){
pair<llo,llo> x=aaa.top();
aaa.pop();
for(llo j=0;j<(llo)adj[x.b].size();j++){
pair<llo,llo> nn=adj[x.b][j];
if(dis[nn.a]==-1 or dis[nn.a]>dis[x.b]+nn.b){
dis[nn.a]=dis[x.b]+nn.b;
aaa.push(mp(dis[nn.a],nn.a));
}
}
}
if(dis[b[1]]==-1){
cout<<-1<<endl;
return 0;
}
if(b[0]==b[1]){
cout<<0<<endl;
return 0;
}
cout<<dis[b[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... |