#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 main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
int b[m];
int p[m];
//map<pair<int,int>,int> aa;
vector<pair<int,int>> adj[n][m+1];
vector<int> vv[n];
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));
}
int dis[n][m+1];
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 |
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 |
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 |
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 |
380 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
1528 KB |
Output is correct |
11 |
Correct |
12 ms |
6136 KB |
Output is correct |
12 |
Correct |
43 ms |
12024 KB |
Output is correct |
13 |
Correct |
44 ms |
12024 KB |
Output is correct |
14 |
Correct |
9 ms |
6008 KB |
Output is correct |
15 |
Correct |
10 ms |
6008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 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 |
6 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
1528 KB |
Output is correct |
11 |
Correct |
10 ms |
6136 KB |
Output is correct |
12 |
Correct |
43 ms |
12024 KB |
Output is correct |
13 |
Correct |
43 ms |
12024 KB |
Output is correct |
14 |
Correct |
9 ms |
6008 KB |
Output is correct |
15 |
Correct |
10 ms |
6008 KB |
Output is correct |
16 |
Correct |
8 ms |
5112 KB |
Output is correct |
17 |
Correct |
39 ms |
37240 KB |
Output is correct |
18 |
Correct |
39 ms |
47992 KB |
Output is correct |
19 |
Correct |
26 ms |
31096 KB |
Output is correct |
20 |
Execution timed out |
1111 ms |
235356 KB |
Time limit exceeded |
21 |
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 |
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 |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
1528 KB |
Output is correct |
11 |
Correct |
11 ms |
6136 KB |
Output is correct |
12 |
Correct |
40 ms |
12024 KB |
Output is correct |
13 |
Correct |
42 ms |
12024 KB |
Output is correct |
14 |
Correct |
9 ms |
6012 KB |
Output is correct |
15 |
Correct |
9 ms |
6008 KB |
Output is correct |
16 |
Correct |
8 ms |
5160 KB |
Output is correct |
17 |
Correct |
40 ms |
37240 KB |
Output is correct |
18 |
Correct |
38 ms |
47992 KB |
Output is correct |
19 |
Correct |
26 ms |
31100 KB |
Output is correct |
20 |
Execution timed out |
1106 ms |
235384 KB |
Time limit exceeded |
21 |
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 |
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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
1528 KB |
Output is correct |
11 |
Correct |
11 ms |
6136 KB |
Output is correct |
12 |
Correct |
40 ms |
12024 KB |
Output is correct |
13 |
Correct |
42 ms |
12024 KB |
Output is correct |
14 |
Correct |
10 ms |
6008 KB |
Output is correct |
15 |
Correct |
10 ms |
6008 KB |
Output is correct |
16 |
Correct |
8 ms |
5112 KB |
Output is correct |
17 |
Correct |
39 ms |
37240 KB |
Output is correct |
18 |
Correct |
38 ms |
47992 KB |
Output is correct |
19 |
Correct |
26 ms |
31096 KB |
Output is correct |
20 |
Execution timed out |
1107 ms |
235640 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |