#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 |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
300 KB |
Output is correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
376 KB |
Output is correct |
2 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |