#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int,int> pp;
typedef vector<int> vec;
typedef vector<vec> mat;
#define pb push_back
#define B 170
int N,M;
int a[30010],P[30010];
int dist[30010];
bool chk[30010][B];
priority_queue<pp,vector<pp>,greater<pp> > Q;
mat doge;
int main(){
scanf("%d%d",&N,&M);
doge.assign(N,vec());
for(int i=0;i<M;i++){
scanf("%d%d",&a[i],&P[i]);
doge[a[i]].push_back(P[i]);
if(P[i]<B){
chk[a[i]][P[i]]=1;
}
}
memset(dist,0x3f,sizeof(dist));
int INF=dist[0];
dist[a[0]]=0;
Q.push(pp(0,a[0]));
int v,d;
while(!Q.empty()){
v=Q.top().second;d=Q.top().first;Q.pop();
if(dist[v]!=d)continue;
if(v==a[1])break;
for(int k=0;k<doge[v].size();k++){
int p=doge[v][k];
for(int i=v+p;i<N;i+=p){
if(dist[i]>dist[v]+(i-v)/p){
dist[i]=dist[v]+(i-v)/p;
Q.push(pp(dist[i],i));
if(p<B&&chk[i][p])break;
if(p<B)chk[i][p]=1;
}
}
for(int i=v-p;i>=0;i-=p){
if(dist[i]>dist[v]+(v-i)/p){
dist[i]=dist[v]+(v-i)/p;
Q.push(pp(dist[i],i));
if(p<B&&chk[i][p])break;
if(p<B)chk[i][p]=1;
}
}
}
}
if(dist[a[1]]==INF){
puts("-1");
}else{
printf("%d",dist[a[1]]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6916 KB |
Output is correct |
2 |
Correct |
0 ms |
6916 KB |
Output is correct |
3 |
Correct |
0 ms |
6916 KB |
Output is correct |
4 |
Correct |
0 ms |
6916 KB |
Output is correct |
5 |
Correct |
0 ms |
6916 KB |
Output is correct |
6 |
Correct |
0 ms |
6916 KB |
Output is correct |
7 |
Correct |
0 ms |
6916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6916 KB |
Output is correct |
2 |
Correct |
0 ms |
6916 KB |
Output is correct |
3 |
Correct |
0 ms |
6916 KB |
Output is correct |
4 |
Correct |
0 ms |
6916 KB |
Output is correct |
5 |
Correct |
0 ms |
6916 KB |
Output is correct |
6 |
Correct |
0 ms |
6916 KB |
Output is correct |
7 |
Correct |
0 ms |
6916 KB |
Output is correct |
8 |
Correct |
0 ms |
6916 KB |
Output is correct |
9 |
Correct |
0 ms |
6916 KB |
Output is correct |
10 |
Correct |
0 ms |
6916 KB |
Output is correct |
11 |
Correct |
0 ms |
6916 KB |
Output is correct |
12 |
Correct |
0 ms |
6916 KB |
Output is correct |
13 |
Correct |
0 ms |
6916 KB |
Output is correct |
14 |
Correct |
0 ms |
6916 KB |
Output is correct |
15 |
Correct |
0 ms |
6916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6916 KB |
Output is correct |
2 |
Correct |
0 ms |
6916 KB |
Output is correct |
3 |
Correct |
0 ms |
6916 KB |
Output is correct |
4 |
Correct |
0 ms |
6916 KB |
Output is correct |
5 |
Correct |
0 ms |
6916 KB |
Output is correct |
6 |
Correct |
0 ms |
6916 KB |
Output is correct |
7 |
Correct |
0 ms |
6916 KB |
Output is correct |
8 |
Correct |
0 ms |
6916 KB |
Output is correct |
9 |
Correct |
0 ms |
6916 KB |
Output is correct |
10 |
Correct |
0 ms |
6916 KB |
Output is correct |
11 |
Correct |
0 ms |
6916 KB |
Output is correct |
12 |
Correct |
0 ms |
6916 KB |
Output is correct |
13 |
Correct |
2 ms |
6916 KB |
Output is correct |
14 |
Correct |
0 ms |
6916 KB |
Output is correct |
15 |
Correct |
0 ms |
6916 KB |
Output is correct |
16 |
Correct |
0 ms |
6916 KB |
Output is correct |
17 |
Correct |
0 ms |
6916 KB |
Output is correct |
18 |
Correct |
0 ms |
6916 KB |
Output is correct |
19 |
Correct |
0 ms |
6916 KB |
Output is correct |
20 |
Correct |
11 ms |
6916 KB |
Output is correct |
21 |
Correct |
0 ms |
6916 KB |
Output is correct |
22 |
Correct |
0 ms |
6916 KB |
Output is correct |
23 |
Correct |
0 ms |
6916 KB |
Output is correct |
24 |
Correct |
0 ms |
6916 KB |
Output is correct |
25 |
Correct |
0 ms |
6916 KB |
Output is correct |
26 |
Correct |
0 ms |
6916 KB |
Output is correct |
27 |
Correct |
0 ms |
6916 KB |
Output is correct |
28 |
Correct |
1 ms |
6916 KB |
Output is correct |
29 |
Correct |
0 ms |
7100 KB |
Output is correct |
30 |
Correct |
0 ms |
7092 KB |
Output is correct |
31 |
Correct |
2 ms |
7096 KB |
Output is correct |
32 |
Correct |
0 ms |
7100 KB |
Output is correct |
33 |
Correct |
4 ms |
7108 KB |
Output is correct |
34 |
Correct |
3 ms |
7108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6916 KB |
Output is correct |
2 |
Correct |
0 ms |
6916 KB |
Output is correct |
3 |
Correct |
0 ms |
6916 KB |
Output is correct |
4 |
Correct |
0 ms |
6916 KB |
Output is correct |
5 |
Correct |
0 ms |
6916 KB |
Output is correct |
6 |
Correct |
0 ms |
6916 KB |
Output is correct |
7 |
Correct |
0 ms |
6916 KB |
Output is correct |
8 |
Correct |
0 ms |
6916 KB |
Output is correct |
9 |
Correct |
0 ms |
6916 KB |
Output is correct |
10 |
Correct |
0 ms |
6916 KB |
Output is correct |
11 |
Correct |
0 ms |
6916 KB |
Output is correct |
12 |
Correct |
0 ms |
6916 KB |
Output is correct |
13 |
Correct |
0 ms |
6916 KB |
Output is correct |
14 |
Correct |
0 ms |
6916 KB |
Output is correct |
15 |
Correct |
0 ms |
6916 KB |
Output is correct |
16 |
Correct |
0 ms |
6916 KB |
Output is correct |
17 |
Correct |
0 ms |
6916 KB |
Output is correct |
18 |
Correct |
0 ms |
6916 KB |
Output is correct |
19 |
Correct |
0 ms |
6916 KB |
Output is correct |
20 |
Correct |
7 ms |
6916 KB |
Output is correct |
21 |
Correct |
0 ms |
6916 KB |
Output is correct |
22 |
Correct |
0 ms |
6916 KB |
Output is correct |
23 |
Correct |
0 ms |
6916 KB |
Output is correct |
24 |
Correct |
0 ms |
6916 KB |
Output is correct |
25 |
Correct |
1 ms |
6916 KB |
Output is correct |
26 |
Correct |
1 ms |
6916 KB |
Output is correct |
27 |
Correct |
1 ms |
6916 KB |
Output is correct |
28 |
Correct |
0 ms |
6916 KB |
Output is correct |
29 |
Correct |
3 ms |
7100 KB |
Output is correct |
30 |
Correct |
0 ms |
7092 KB |
Output is correct |
31 |
Correct |
2 ms |
7096 KB |
Output is correct |
32 |
Correct |
1 ms |
7100 KB |
Output is correct |
33 |
Correct |
4 ms |
7108 KB |
Output is correct |
34 |
Correct |
3 ms |
7108 KB |
Output is correct |
35 |
Correct |
9 ms |
7180 KB |
Output is correct |
36 |
Correct |
0 ms |
6916 KB |
Output is correct |
37 |
Correct |
6 ms |
7192 KB |
Output is correct |
38 |
Correct |
10 ms |
7324 KB |
Output is correct |
39 |
Correct |
6 ms |
7180 KB |
Output is correct |
40 |
Correct |
9 ms |
7180 KB |
Output is correct |
41 |
Correct |
12 ms |
7324 KB |
Output is correct |
42 |
Correct |
5 ms |
7096 KB |
Output is correct |
43 |
Correct |
0 ms |
7096 KB |
Output is correct |
44 |
Correct |
301 ms |
7092 KB |
Output is correct |
45 |
Correct |
11 ms |
7336 KB |
Output is correct |
46 |
Correct |
12 ms |
7332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
6916 KB |
Output is correct |
2 |
Correct |
0 ms |
6916 KB |
Output is correct |
3 |
Correct |
0 ms |
6916 KB |
Output is correct |
4 |
Correct |
0 ms |
6916 KB |
Output is correct |
5 |
Correct |
0 ms |
6916 KB |
Output is correct |
6 |
Correct |
0 ms |
6916 KB |
Output is correct |
7 |
Correct |
0 ms |
6916 KB |
Output is correct |
8 |
Correct |
0 ms |
6916 KB |
Output is correct |
9 |
Correct |
0 ms |
6916 KB |
Output is correct |
10 |
Correct |
0 ms |
6916 KB |
Output is correct |
11 |
Correct |
0 ms |
6916 KB |
Output is correct |
12 |
Correct |
0 ms |
6916 KB |
Output is correct |
13 |
Correct |
0 ms |
6916 KB |
Output is correct |
14 |
Correct |
0 ms |
6916 KB |
Output is correct |
15 |
Correct |
1 ms |
6916 KB |
Output is correct |
16 |
Correct |
0 ms |
6916 KB |
Output is correct |
17 |
Correct |
1 ms |
6916 KB |
Output is correct |
18 |
Correct |
0 ms |
6916 KB |
Output is correct |
19 |
Correct |
0 ms |
6916 KB |
Output is correct |
20 |
Correct |
11 ms |
6916 KB |
Output is correct |
21 |
Correct |
0 ms |
6916 KB |
Output is correct |
22 |
Correct |
0 ms |
6916 KB |
Output is correct |
23 |
Correct |
0 ms |
6916 KB |
Output is correct |
24 |
Correct |
2 ms |
6916 KB |
Output is correct |
25 |
Correct |
0 ms |
6916 KB |
Output is correct |
26 |
Correct |
0 ms |
6916 KB |
Output is correct |
27 |
Correct |
0 ms |
6916 KB |
Output is correct |
28 |
Correct |
0 ms |
6916 KB |
Output is correct |
29 |
Correct |
0 ms |
7100 KB |
Output is correct |
30 |
Correct |
0 ms |
7092 KB |
Output is correct |
31 |
Correct |
0 ms |
7096 KB |
Output is correct |
32 |
Correct |
0 ms |
7100 KB |
Output is correct |
33 |
Correct |
4 ms |
7108 KB |
Output is correct |
34 |
Correct |
3 ms |
7108 KB |
Output is correct |
35 |
Correct |
0 ms |
7180 KB |
Output is correct |
36 |
Correct |
0 ms |
6916 KB |
Output is correct |
37 |
Correct |
9 ms |
7192 KB |
Output is correct |
38 |
Correct |
12 ms |
7324 KB |
Output is correct |
39 |
Correct |
0 ms |
7180 KB |
Output is correct |
40 |
Correct |
0 ms |
7180 KB |
Output is correct |
41 |
Correct |
6 ms |
7324 KB |
Output is correct |
42 |
Correct |
0 ms |
7096 KB |
Output is correct |
43 |
Correct |
0 ms |
7096 KB |
Output is correct |
44 |
Correct |
300 ms |
7092 KB |
Output is correct |
45 |
Correct |
10 ms |
7336 KB |
Output is correct |
46 |
Correct |
6 ms |
7332 KB |
Output is correct |
47 |
Correct |
12 ms |
8324 KB |
Output is correct |
48 |
Correct |
9 ms |
7860 KB |
Output is correct |
49 |
Correct |
9 ms |
7908 KB |
Output is correct |
50 |
Correct |
6 ms |
7836 KB |
Output is correct |
51 |
Correct |
20 ms |
8984 KB |
Output is correct |
52 |
Correct |
22 ms |
8988 KB |
Output is correct |
53 |
Correct |
15 ms |
8408 KB |
Output is correct |
54 |
Correct |
4 ms |
7752 KB |
Output is correct |
55 |
Correct |
0 ms |
7752 KB |
Output is correct |
56 |
Execution timed out |
1000 ms |
8540 KB |
Program timed out |
57 |
Halted |
0 ms |
0 KB |
- |