#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
int dis[2020][2020];
int b[2020];
int p[2020];
vector<pair<int,int>> adj[2020][2020];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
//map<pair<int,int><int,int>,int> aa;
for(int 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]));
}
memset(dis,-1,sizeof(dis));
//dis[b[0]][2010]=0;
dis[b[0]][p[0]]=0;
deque<pair<int,int>> aaa;
aaa.push_back(mp(b[0],p[0]));
//aaa.push_back(mp(b[0],2010));
while(!aaa.empty()){
pair<int,int> x;
x=aaa.front();
// cout<<x.a<<" "<<x.b<<" "<<dis[x.a][x.b]<<endl;
aaa.pop_front();
int co=1;
if(x.b==2010){
co=0;
}
for(auto nn:adj[x.a][x.b]){
if(dis[nn.a][nn.b]==-1 or dis[nn.a][nn.b]>dis[x.a][x.b]+co){
dis[nn.a][nn.b]=dis[x.a][x.b]+co;
if(co==0){
aaa.push_front(nn);
}
else{
aaa.push_back(nn);
}
}
}
}
/* if(b[0]==b[1]){
cout<<0<<endl;
return 0;
}*/
cout<<dis[b[1]][p[1]]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
112120 KB |
Output is correct |
2 |
Correct |
76 ms |
112120 KB |
Output is correct |
3 |
Correct |
77 ms |
112248 KB |
Output is correct |
4 |
Correct |
78 ms |
112176 KB |
Output is correct |
5 |
Correct |
77 ms |
112120 KB |
Output is correct |
6 |
Correct |
76 ms |
112168 KB |
Output is correct |
7 |
Correct |
84 ms |
112124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
112120 KB |
Output is correct |
2 |
Correct |
75 ms |
112120 KB |
Output is correct |
3 |
Correct |
75 ms |
112120 KB |
Output is correct |
4 |
Correct |
77 ms |
112120 KB |
Output is correct |
5 |
Correct |
75 ms |
112120 KB |
Output is correct |
6 |
Correct |
76 ms |
112120 KB |
Output is correct |
7 |
Correct |
76 ms |
112120 KB |
Output is correct |
8 |
Correct |
77 ms |
112120 KB |
Output is correct |
9 |
Correct |
77 ms |
112192 KB |
Output is correct |
10 |
Correct |
78 ms |
112120 KB |
Output is correct |
11 |
Incorrect |
77 ms |
112376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
112120 KB |
Output is correct |
2 |
Correct |
81 ms |
112120 KB |
Output is correct |
3 |
Correct |
76 ms |
112120 KB |
Output is correct |
4 |
Correct |
81 ms |
112252 KB |
Output is correct |
5 |
Correct |
78 ms |
112120 KB |
Output is correct |
6 |
Correct |
75 ms |
112120 KB |
Output is correct |
7 |
Correct |
78 ms |
112120 KB |
Output is correct |
8 |
Correct |
76 ms |
112120 KB |
Output is correct |
9 |
Correct |
77 ms |
112120 KB |
Output is correct |
10 |
Correct |
76 ms |
112120 KB |
Output is correct |
11 |
Incorrect |
78 ms |
112376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
112124 KB |
Output is correct |
2 |
Correct |
78 ms |
112120 KB |
Output is correct |
3 |
Correct |
78 ms |
112176 KB |
Output is correct |
4 |
Correct |
78 ms |
112120 KB |
Output is correct |
5 |
Correct |
79 ms |
112116 KB |
Output is correct |
6 |
Correct |
76 ms |
112120 KB |
Output is correct |
7 |
Correct |
77 ms |
112120 KB |
Output is correct |
8 |
Correct |
77 ms |
112120 KB |
Output is correct |
9 |
Correct |
78 ms |
112120 KB |
Output is correct |
10 |
Correct |
80 ms |
112248 KB |
Output is correct |
11 |
Incorrect |
79 ms |
112376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
112120 KB |
Output is correct |
2 |
Correct |
76 ms |
112120 KB |
Output is correct |
3 |
Correct |
76 ms |
112124 KB |
Output is correct |
4 |
Correct |
81 ms |
112104 KB |
Output is correct |
5 |
Correct |
77 ms |
112120 KB |
Output is correct |
6 |
Correct |
76 ms |
112120 KB |
Output is correct |
7 |
Correct |
76 ms |
112120 KB |
Output is correct |
8 |
Correct |
77 ms |
112120 KB |
Output is correct |
9 |
Correct |
81 ms |
112276 KB |
Output is correct |
10 |
Correct |
79 ms |
112120 KB |
Output is correct |
11 |
Incorrect |
79 ms |
112376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |