#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][m+1];
vector<llo> vv[n];
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]][i].pb(mp(j,i));
adj[j+p[i]][i].pb(mp(j,m));
}
for(llo j=b[i]+p[i];j<n;j=j+p[i]){
adj[j-p[i]][i].pb(mp(j,i));
adj[j-p[i]][i].pb(mp(j,m));
}
adj[b[i]][m].pb(mp(b[i],i));
}
llo dis[n][m+1];
for(llo i=0;i<n;i++){
for(llo j=0;j<m+1;j++){
dis[i][j]=-1;
}
}
dis[b[0]][m]=0;
dis[b[0]][0]=0;
queue<pair<llo,pair<llo,llo>>> aaa;
aaa.push(mp(0,mp(b[0],0)));
aaa.push(mp(0,mp(b[0],m)));
while(aaa.size()>0){
pair<llo,pair<llo,llo>> x=aaa.front();
aaa.pop();
x.a=-x.a;
llo co=1;
if(x.b.b==m){
co=0;
}
for(llo j=0;j<(llo)adj[x.b.a][x.b.b].size();j++){
pair<llo,llo> nn=adj[x.b.a][x.b.b][j];
if(dis[nn.a][nn.b]==-1){
dis[nn.a][nn.b]=dis[x.b.a][x.b.b]+co;
aaa.push(mp(-dis[nn.a][nn.b],mp(nn.a,nn.b)));
}
}
}
if(b[0]==b[1]){
cout<<0<<endl;
return 0;
}
cout<<dis[b[1]][m]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
372 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
280 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Incorrect |
5 ms |
380 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
380 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |