#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;
deque<pair<llo,pair<llo,llo>>> aaa;
aaa.push_back(mp(0,mp(b[0],0)));
aaa.push_back(mp(0,mp(b[0],m)));
while(aaa.size()>0){
pair<llo,pair<llo,llo>> x=aaa.front();
aaa.pop_front();
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]>x.a+co or dis[nn.a][nn.b]==-1){
dis[nn.a][nn.b]=dis[x.b.a][x.b.b]+co;
if(co==0){
aaa.push_front(mp(-dis[nn.a][nn.b],mp(nn.a,nn.b)));
}
else{
aaa.push_back(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 |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
380 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 |
376 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 |
1656 KB |
Output is correct |
11 |
Correct |
11 ms |
7032 KB |
Output is correct |
12 |
Correct |
47 ms |
15864 KB |
Output is correct |
13 |
Correct |
51 ms |
16192 KB |
Output is correct |
14 |
Correct |
10 ms |
6904 KB |
Output is correct |
15 |
Correct |
10 ms |
6904 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 |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
380 KB |
Output is correct |
10 |
Correct |
6 ms |
1656 KB |
Output is correct |
11 |
Correct |
12 ms |
7288 KB |
Output is correct |
12 |
Correct |
47 ms |
15864 KB |
Output is correct |
13 |
Correct |
48 ms |
15992 KB |
Output is correct |
14 |
Correct |
10 ms |
6904 KB |
Output is correct |
15 |
Correct |
10 ms |
6908 KB |
Output is correct |
16 |
Correct |
10 ms |
5880 KB |
Output is correct |
17 |
Correct |
43 ms |
42744 KB |
Output is correct |
18 |
Correct |
42 ms |
54776 KB |
Output is correct |
19 |
Correct |
30 ms |
35448 KB |
Output is correct |
20 |
Runtime error |
550 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
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 |
256 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 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
1660 KB |
Output is correct |
11 |
Correct |
11 ms |
7032 KB |
Output is correct |
12 |
Correct |
49 ms |
15864 KB |
Output is correct |
13 |
Correct |
49 ms |
15992 KB |
Output is correct |
14 |
Correct |
10 ms |
6904 KB |
Output is correct |
15 |
Correct |
11 ms |
6904 KB |
Output is correct |
16 |
Correct |
9 ms |
5880 KB |
Output is correct |
17 |
Correct |
42 ms |
42744 KB |
Output is correct |
18 |
Correct |
41 ms |
54780 KB |
Output is correct |
19 |
Correct |
28 ms |
35436 KB |
Output is correct |
20 |
Runtime error |
534 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
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 |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
552 KB |
Output is correct |
10 |
Correct |
6 ms |
1912 KB |
Output is correct |
11 |
Correct |
12 ms |
7160 KB |
Output is correct |
12 |
Correct |
47 ms |
15864 KB |
Output is correct |
13 |
Correct |
52 ms |
15992 KB |
Output is correct |
14 |
Correct |
10 ms |
6904 KB |
Output is correct |
15 |
Correct |
10 ms |
6904 KB |
Output is correct |
16 |
Correct |
9 ms |
5880 KB |
Output is correct |
17 |
Correct |
43 ms |
42744 KB |
Output is correct |
18 |
Correct |
42 ms |
54904 KB |
Output is correct |
19 |
Correct |
29 ms |
35576 KB |
Output is correct |
20 |
Runtime error |
539 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
21 |
Halted |
0 ms |
0 KB |
- |