#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//typedef int64_t int;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl "\n"
int dis[2001][30001];
vector<pair<int,int>> adj[2001][30001];
int b[30001];
int p[30001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
//map<pair<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]][i].pb(mp(j,i));
adj[j+p[i]][i].pb(mp(j,m));
}
for(int 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));
}
memset(dis,-1,sizeof(dis));
dis[b[0]][m]=0;
dis[b[0]][0]=0;
deque<pair<int,int>> aaa;
aaa.push_back(mp(b[0],0));
aaa.push_back(mp(b[0],m));
while(!aaa.empty()){
pair<int,int> x;
x=aaa.front();
aaa.pop_front();
int co=1;
if(x.b==m){
co=0;
}
for(auto nn:adj[x.a][x.b]){
if(dis[nn.a][nn.b]==-1){
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]][m]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
156 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
156 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
155 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
151 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
155 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |