#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//typedef int64_t int;
//#define mp make_pairr
#define pb push_back
#define int64_t int
/*#define a first
#define b second*/
#define endl "\n"
int dis[2020][2020];
int b[2020];
int p[2020];
struct pairr{
int a;
int b;
};
vector<pairr> adj[2020][2020];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
//map<pairr<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]){
pairr ac;
ac.a=j;
ac.b=p[i];
adj[j+p[i]][p[i]].pb(ac);
ac.b=2010;
adj[j+p[i]][p[i]].pb(ac);
}
for(int j=b[i]+p[i];j<n;j=j+p[i]){
pairr ac;
ac.a=j;
ac.b=p[i];
adj[j-p[i]][p[i]].pb(ac);
ac.b=2010;
adj[j-p[i]][p[i]].pb(ac);
}
pairr ac;
ac.a=b[i];
ac.b=p[i];
adj[b[i]][2010].pb(ac);
}
memset(dis,-1,sizeof(dis));
dis[b[0]][2010]=0;
dis[b[0]][p[0]]=0;
deque<pairr> aaa;
pairr ac;
ac.a=b[0];
ac.b=p[0];
aaa.push_back(ac);
ac.b=2010;
aaa.push_back(ac);
while(!aaa.empty()){
pairr 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]][2010]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
112152 KB |
Output is correct |
2 |
Correct |
76 ms |
112120 KB |
Output is correct |
3 |
Correct |
77 ms |
112120 KB |
Output is correct |
4 |
Correct |
76 ms |
112160 KB |
Output is correct |
5 |
Correct |
77 ms |
112124 KB |
Output is correct |
6 |
Correct |
77 ms |
112120 KB |
Output is correct |
7 |
Correct |
75 ms |
112096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
112120 KB |
Output is correct |
2 |
Correct |
77 ms |
112252 KB |
Output is correct |
3 |
Correct |
81 ms |
112120 KB |
Output is correct |
4 |
Correct |
76 ms |
112176 KB |
Output is correct |
5 |
Correct |
77 ms |
112120 KB |
Output is correct |
6 |
Correct |
79 ms |
112160 KB |
Output is correct |
7 |
Correct |
79 ms |
112120 KB |
Output is correct |
8 |
Correct |
78 ms |
112120 KB |
Output is correct |
9 |
Correct |
77 ms |
112120 KB |
Output is correct |
10 |
Correct |
77 ms |
112120 KB |
Output is correct |
11 |
Correct |
79 ms |
112376 KB |
Output is correct |
12 |
Correct |
84 ms |
115376 KB |
Output is correct |
13 |
Correct |
84 ms |
115576 KB |
Output is correct |
14 |
Correct |
78 ms |
112244 KB |
Output is correct |
15 |
Correct |
77 ms |
112248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
112176 KB |
Output is correct |
2 |
Correct |
76 ms |
112120 KB |
Output is correct |
3 |
Correct |
77 ms |
112180 KB |
Output is correct |
4 |
Correct |
77 ms |
112120 KB |
Output is correct |
5 |
Correct |
77 ms |
112120 KB |
Output is correct |
6 |
Correct |
77 ms |
112248 KB |
Output is correct |
7 |
Correct |
76 ms |
112180 KB |
Output is correct |
8 |
Correct |
76 ms |
112120 KB |
Output is correct |
9 |
Correct |
78 ms |
112124 KB |
Output is correct |
10 |
Correct |
81 ms |
112248 KB |
Output is correct |
11 |
Correct |
78 ms |
112376 KB |
Output is correct |
12 |
Correct |
85 ms |
115448 KB |
Output is correct |
13 |
Correct |
84 ms |
115576 KB |
Output is correct |
14 |
Correct |
78 ms |
112376 KB |
Output is correct |
15 |
Correct |
82 ms |
112248 KB |
Output is correct |
16 |
Correct |
77 ms |
112248 KB |
Output is correct |
17 |
Correct |
84 ms |
112760 KB |
Output is correct |
18 |
Correct |
79 ms |
112376 KB |
Output is correct |
19 |
Correct |
79 ms |
112248 KB |
Output is correct |
20 |
Correct |
411 ms |
181120 KB |
Output is correct |
21 |
Correct |
78 ms |
112252 KB |
Output is correct |
22 |
Correct |
80 ms |
112248 KB |
Output is correct |
23 |
Correct |
82 ms |
112380 KB |
Output is correct |
24 |
Correct |
84 ms |
112508 KB |
Output is correct |
25 |
Correct |
82 ms |
112504 KB |
Output is correct |
26 |
Correct |
399 ms |
176760 KB |
Output is correct |
27 |
Correct |
381 ms |
175224 KB |
Output is correct |
28 |
Correct |
86 ms |
112888 KB |
Output is correct |
29 |
Correct |
100 ms |
114040 KB |
Output is correct |
30 |
Correct |
87 ms |
112760 KB |
Output is correct |
31 |
Correct |
92 ms |
113272 KB |
Output is correct |
32 |
Correct |
87 ms |
112888 KB |
Output is correct |
33 |
Correct |
125 ms |
115832 KB |
Output is correct |
34 |
Correct |
126 ms |
115960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
112168 KB |
Output is correct |
2 |
Correct |
80 ms |
112120 KB |
Output is correct |
3 |
Correct |
77 ms |
112248 KB |
Output is correct |
4 |
Correct |
76 ms |
112120 KB |
Output is correct |
5 |
Correct |
77 ms |
112124 KB |
Output is correct |
6 |
Correct |
78 ms |
112120 KB |
Output is correct |
7 |
Correct |
81 ms |
112120 KB |
Output is correct |
8 |
Correct |
77 ms |
112120 KB |
Output is correct |
9 |
Correct |
77 ms |
112120 KB |
Output is correct |
10 |
Correct |
77 ms |
112120 KB |
Output is correct |
11 |
Correct |
79 ms |
112504 KB |
Output is correct |
12 |
Correct |
86 ms |
115540 KB |
Output is correct |
13 |
Correct |
84 ms |
115576 KB |
Output is correct |
14 |
Correct |
78 ms |
112376 KB |
Output is correct |
15 |
Correct |
78 ms |
112248 KB |
Output is correct |
16 |
Correct |
79 ms |
112376 KB |
Output is correct |
17 |
Correct |
88 ms |
112888 KB |
Output is correct |
18 |
Correct |
79 ms |
112376 KB |
Output is correct |
19 |
Correct |
79 ms |
112248 KB |
Output is correct |
20 |
Correct |
411 ms |
180984 KB |
Output is correct |
21 |
Correct |
81 ms |
112248 KB |
Output is correct |
22 |
Correct |
81 ms |
112376 KB |
Output is correct |
23 |
Correct |
81 ms |
112376 KB |
Output is correct |
24 |
Correct |
83 ms |
112504 KB |
Output is correct |
25 |
Correct |
81 ms |
112376 KB |
Output is correct |
26 |
Correct |
387 ms |
176628 KB |
Output is correct |
27 |
Correct |
375 ms |
175096 KB |
Output is correct |
28 |
Correct |
85 ms |
112888 KB |
Output is correct |
29 |
Correct |
103 ms |
114040 KB |
Output is correct |
30 |
Correct |
83 ms |
112760 KB |
Output is correct |
31 |
Correct |
90 ms |
113144 KB |
Output is correct |
32 |
Correct |
84 ms |
112888 KB |
Output is correct |
33 |
Correct |
127 ms |
115960 KB |
Output is correct |
34 |
Correct |
128 ms |
115960 KB |
Output is correct |
35 |
Incorrect |
133 ms |
115980 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
112120 KB |
Output is correct |
2 |
Correct |
78 ms |
112220 KB |
Output is correct |
3 |
Correct |
77 ms |
112120 KB |
Output is correct |
4 |
Correct |
79 ms |
112120 KB |
Output is correct |
5 |
Correct |
79 ms |
112248 KB |
Output is correct |
6 |
Correct |
82 ms |
112120 KB |
Output is correct |
7 |
Correct |
80 ms |
112120 KB |
Output is correct |
8 |
Correct |
78 ms |
112120 KB |
Output is correct |
9 |
Correct |
81 ms |
112120 KB |
Output is correct |
10 |
Correct |
78 ms |
112208 KB |
Output is correct |
11 |
Correct |
78 ms |
112376 KB |
Output is correct |
12 |
Correct |
87 ms |
115448 KB |
Output is correct |
13 |
Correct |
84 ms |
115576 KB |
Output is correct |
14 |
Correct |
80 ms |
112248 KB |
Output is correct |
15 |
Correct |
79 ms |
112440 KB |
Output is correct |
16 |
Correct |
78 ms |
112248 KB |
Output is correct |
17 |
Correct |
87 ms |
112760 KB |
Output is correct |
18 |
Correct |
77 ms |
112376 KB |
Output is correct |
19 |
Correct |
80 ms |
112252 KB |
Output is correct |
20 |
Correct |
394 ms |
180984 KB |
Output is correct |
21 |
Correct |
77 ms |
112248 KB |
Output is correct |
22 |
Correct |
77 ms |
112376 KB |
Output is correct |
23 |
Correct |
81 ms |
112376 KB |
Output is correct |
24 |
Correct |
84 ms |
112504 KB |
Output is correct |
25 |
Correct |
81 ms |
112376 KB |
Output is correct |
26 |
Correct |
388 ms |
176632 KB |
Output is correct |
27 |
Correct |
384 ms |
175324 KB |
Output is correct |
28 |
Correct |
85 ms |
112888 KB |
Output is correct |
29 |
Correct |
99 ms |
114040 KB |
Output is correct |
30 |
Correct |
83 ms |
112632 KB |
Output is correct |
31 |
Correct |
91 ms |
113272 KB |
Output is correct |
32 |
Correct |
87 ms |
112888 KB |
Output is correct |
33 |
Correct |
122 ms |
115960 KB |
Output is correct |
34 |
Correct |
126 ms |
116088 KB |
Output is correct |
35 |
Incorrect |
132 ms |
115960 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |