#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
int N,M;
int a[30010],P[30010];
int dist[30010];
bool chk[30010];
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]);
}
memset(dist,0x3f,sizeof(dist));
int INF=dist[0];
dist[a[0]]=0;
Q.push(pp(0,a[0]));
int v;
while(!Q.empty()){
do{
v=Q.top().second;Q.pop();
}while(!Q.empty()&&chk[v]);
chk[v]=1;
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(abs(v-i)%p==0){
if(dist[i]>dist[v]+abs(v-i)/p){
dist[i]=dist[v]+abs(v-i)/p;
Q.push(pp(dist[i],i));
}
}
}
for(int i=v-p;i>=0;i-=p){
if(abs(v-i)%p==0){
if(dist[i]>dist[v]+abs(v-i)/p){
dist[i]=dist[v]+abs(v-i)/p;
Q.push(pp(dist[i],i));
}
}
}
}
}
if(dist[a[1]]==INF){
puts("-1");
}else{
printf("%d",dist[a[1]]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
0 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
2 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
1 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
0 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2104 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
1 ms |
1960 KB |
Output is correct |
24 |
Correct |
0 ms |
1960 KB |
Output is correct |
25 |
Correct |
1 ms |
1960 KB |
Output is correct |
26 |
Correct |
0 ms |
1960 KB |
Output is correct |
27 |
Correct |
0 ms |
1960 KB |
Output is correct |
28 |
Correct |
0 ms |
1960 KB |
Output is correct |
29 |
Correct |
3 ms |
2144 KB |
Output is correct |
30 |
Correct |
0 ms |
2136 KB |
Output is correct |
31 |
Correct |
1 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2144 KB |
Output is correct |
33 |
Correct |
3 ms |
2152 KB |
Output is correct |
34 |
Correct |
3 ms |
2152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
0 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
0 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2104 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
1 ms |
1960 KB |
Output is correct |
24 |
Correct |
1 ms |
1960 KB |
Output is correct |
25 |
Correct |
0 ms |
1960 KB |
Output is correct |
26 |
Correct |
1 ms |
1960 KB |
Output is correct |
27 |
Correct |
0 ms |
1960 KB |
Output is correct |
28 |
Correct |
0 ms |
1960 KB |
Output is correct |
29 |
Correct |
3 ms |
2144 KB |
Output is correct |
30 |
Correct |
1 ms |
2136 KB |
Output is correct |
31 |
Correct |
2 ms |
2140 KB |
Output is correct |
32 |
Correct |
1 ms |
2144 KB |
Output is correct |
33 |
Correct |
4 ms |
2152 KB |
Output is correct |
34 |
Correct |
0 ms |
2152 KB |
Output is correct |
35 |
Correct |
10 ms |
2224 KB |
Output is correct |
36 |
Correct |
1 ms |
1960 KB |
Output is correct |
37 |
Correct |
9 ms |
2236 KB |
Output is correct |
38 |
Correct |
11 ms |
2368 KB |
Output is correct |
39 |
Correct |
0 ms |
2224 KB |
Output is correct |
40 |
Correct |
8 ms |
2224 KB |
Output is correct |
41 |
Correct |
0 ms |
2368 KB |
Output is correct |
42 |
Correct |
0 ms |
2140 KB |
Output is correct |
43 |
Correct |
0 ms |
2140 KB |
Output is correct |
44 |
Correct |
335 ms |
2136 KB |
Output is correct |
45 |
Correct |
13 ms |
2380 KB |
Output is correct |
46 |
Correct |
12 ms |
2376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
0 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
0 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2104 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
1 ms |
1960 KB |
Output is correct |
24 |
Correct |
0 ms |
1960 KB |
Output is correct |
25 |
Correct |
0 ms |
1960 KB |
Output is correct |
26 |
Correct |
0 ms |
1960 KB |
Output is correct |
27 |
Correct |
0 ms |
1960 KB |
Output is correct |
28 |
Correct |
0 ms |
1960 KB |
Output is correct |
29 |
Correct |
3 ms |
2144 KB |
Output is correct |
30 |
Correct |
0 ms |
2136 KB |
Output is correct |
31 |
Correct |
0 ms |
2140 KB |
Output is correct |
32 |
Correct |
0 ms |
2144 KB |
Output is correct |
33 |
Correct |
0 ms |
2152 KB |
Output is correct |
34 |
Correct |
3 ms |
2152 KB |
Output is correct |
35 |
Correct |
10 ms |
2224 KB |
Output is correct |
36 |
Correct |
0 ms |
1960 KB |
Output is correct |
37 |
Correct |
8 ms |
2236 KB |
Output is correct |
38 |
Correct |
9 ms |
2368 KB |
Output is correct |
39 |
Correct |
5 ms |
2224 KB |
Output is correct |
40 |
Correct |
10 ms |
2224 KB |
Output is correct |
41 |
Correct |
10 ms |
2368 KB |
Output is correct |
42 |
Correct |
7 ms |
2140 KB |
Output is correct |
43 |
Correct |
7 ms |
2140 KB |
Output is correct |
44 |
Correct |
337 ms |
2136 KB |
Output is correct |
45 |
Correct |
9 ms |
2380 KB |
Output is correct |
46 |
Correct |
12 ms |
2376 KB |
Output is correct |
47 |
Correct |
17 ms |
3368 KB |
Output is correct |
48 |
Correct |
10 ms |
2904 KB |
Output is correct |
49 |
Correct |
4 ms |
2952 KB |
Output is correct |
50 |
Correct |
0 ms |
2880 KB |
Output is correct |
51 |
Correct |
18 ms |
4796 KB |
Output is correct |
52 |
Correct |
16 ms |
4032 KB |
Output is correct |
53 |
Correct |
0 ms |
3452 KB |
Output is correct |
54 |
Correct |
0 ms |
2796 KB |
Output is correct |
55 |
Correct |
3 ms |
2796 KB |
Output is correct |
56 |
Execution timed out |
1000 ms |
3988 KB |
Program timed out |
57 |
Halted |
0 ms |
0 KB |
- |