#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int,int> data;
data a[30010];
int st[30010],D[30010];
priority_queue<data,vector<data>,greater<data> > q;
int main(){
int i,j,n,m,s,e,d;
scanf("%d%d",&n,&m);
for(i=0;i<m;++i)scanf("%d%d",&a[i].f,&a[i].s);
s=a[0].f,e=a[1].f;
sort(a,a+m);
st[a[0].f]=0;
for(i=1;i<m;++i)if(a[i].f!=a[i-1].f)st[a[i].f]=i;
for(i=0;i<n;++i)D[i]=2e9;
D[s]=0;
q.push({0,s});
while(!q.empty()){
data p=q.top();
q.pop();
if(D[p.s]!=p.f)continue;
for(i=st[p.s];a[i].f==p.s&&i<m;++i){
d=p.f;
for(j=p.s+a[i].s;j<n;j+=a[i].s)if(D[j]>++d)q.push({D[j]=d,j});
d=p.f;
for(j=p.s-a[i].s;j>=0;j-=a[i].s)if(D[j]>++d)q.push({D[j]=d,j});
}
}
printf("%d",D[e]<2e9?D[e]:-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2188 KB |
Output is correct |
2 |
Correct |
0 ms |
2188 KB |
Output is correct |
3 |
Correct |
0 ms |
2188 KB |
Output is correct |
4 |
Correct |
0 ms |
2188 KB |
Output is correct |
5 |
Correct |
0 ms |
2188 KB |
Output is correct |
6 |
Correct |
0 ms |
2188 KB |
Output is correct |
7 |
Correct |
0 ms |
2188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2188 KB |
Output is correct |
2 |
Correct |
0 ms |
2188 KB |
Output is correct |
3 |
Correct |
0 ms |
2188 KB |
Output is correct |
4 |
Correct |
0 ms |
2188 KB |
Output is correct |
5 |
Correct |
0 ms |
2188 KB |
Output is correct |
6 |
Correct |
0 ms |
2188 KB |
Output is correct |
7 |
Correct |
0 ms |
2188 KB |
Output is correct |
8 |
Correct |
0 ms |
2188 KB |
Output is correct |
9 |
Correct |
0 ms |
2188 KB |
Output is correct |
10 |
Correct |
0 ms |
2188 KB |
Output is correct |
11 |
Correct |
0 ms |
2188 KB |
Output is correct |
12 |
Correct |
1 ms |
2188 KB |
Output is correct |
13 |
Correct |
0 ms |
2188 KB |
Output is correct |
14 |
Correct |
0 ms |
2188 KB |
Output is correct |
15 |
Correct |
0 ms |
2188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2188 KB |
Output is correct |
2 |
Correct |
0 ms |
2188 KB |
Output is correct |
3 |
Correct |
0 ms |
2188 KB |
Output is correct |
4 |
Correct |
0 ms |
2188 KB |
Output is correct |
5 |
Correct |
0 ms |
2188 KB |
Output is correct |
6 |
Correct |
0 ms |
2188 KB |
Output is correct |
7 |
Correct |
0 ms |
2188 KB |
Output is correct |
8 |
Correct |
0 ms |
2188 KB |
Output is correct |
9 |
Correct |
0 ms |
2188 KB |
Output is correct |
10 |
Correct |
0 ms |
2188 KB |
Output is correct |
11 |
Correct |
0 ms |
2188 KB |
Output is correct |
12 |
Correct |
0 ms |
2188 KB |
Output is correct |
13 |
Correct |
0 ms |
2188 KB |
Output is correct |
14 |
Correct |
0 ms |
2188 KB |
Output is correct |
15 |
Correct |
0 ms |
2188 KB |
Output is correct |
16 |
Correct |
0 ms |
2188 KB |
Output is correct |
17 |
Correct |
0 ms |
2188 KB |
Output is correct |
18 |
Correct |
0 ms |
2188 KB |
Output is correct |
19 |
Correct |
0 ms |
2188 KB |
Output is correct |
20 |
Correct |
5 ms |
2188 KB |
Output is correct |
21 |
Correct |
0 ms |
2188 KB |
Output is correct |
22 |
Correct |
0 ms |
2188 KB |
Output is correct |
23 |
Correct |
1 ms |
2188 KB |
Output is correct |
24 |
Correct |
2 ms |
2188 KB |
Output is correct |
25 |
Correct |
0 ms |
2188 KB |
Output is correct |
26 |
Correct |
5 ms |
2188 KB |
Output is correct |
27 |
Correct |
0 ms |
2188 KB |
Output is correct |
28 |
Correct |
0 ms |
2188 KB |
Output is correct |
29 |
Correct |
0 ms |
2320 KB |
Output is correct |
30 |
Correct |
1 ms |
2188 KB |
Output is correct |
31 |
Correct |
0 ms |
2188 KB |
Output is correct |
32 |
Correct |
0 ms |
2188 KB |
Output is correct |
33 |
Correct |
0 ms |
2576 KB |
Output is correct |
34 |
Correct |
0 ms |
2576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2188 KB |
Output is correct |
2 |
Correct |
0 ms |
2188 KB |
Output is correct |
3 |
Correct |
0 ms |
2188 KB |
Output is correct |
4 |
Correct |
0 ms |
2188 KB |
Output is correct |
5 |
Correct |
0 ms |
2188 KB |
Output is correct |
6 |
Correct |
0 ms |
2188 KB |
Output is correct |
7 |
Correct |
0 ms |
2188 KB |
Output is correct |
8 |
Correct |
0 ms |
2188 KB |
Output is correct |
9 |
Correct |
0 ms |
2188 KB |
Output is correct |
10 |
Correct |
0 ms |
2188 KB |
Output is correct |
11 |
Correct |
0 ms |
2188 KB |
Output is correct |
12 |
Correct |
0 ms |
2188 KB |
Output is correct |
13 |
Correct |
0 ms |
2188 KB |
Output is correct |
14 |
Correct |
0 ms |
2188 KB |
Output is correct |
15 |
Correct |
1 ms |
2188 KB |
Output is correct |
16 |
Correct |
0 ms |
2188 KB |
Output is correct |
17 |
Correct |
1 ms |
2188 KB |
Output is correct |
18 |
Correct |
0 ms |
2188 KB |
Output is correct |
19 |
Correct |
0 ms |
2188 KB |
Output is correct |
20 |
Correct |
5 ms |
2188 KB |
Output is correct |
21 |
Correct |
0 ms |
2188 KB |
Output is correct |
22 |
Correct |
0 ms |
2188 KB |
Output is correct |
23 |
Correct |
0 ms |
2188 KB |
Output is correct |
24 |
Correct |
1 ms |
2188 KB |
Output is correct |
25 |
Correct |
2 ms |
2188 KB |
Output is correct |
26 |
Correct |
5 ms |
2188 KB |
Output is correct |
27 |
Correct |
4 ms |
2188 KB |
Output is correct |
28 |
Correct |
0 ms |
2188 KB |
Output is correct |
29 |
Correct |
0 ms |
2320 KB |
Output is correct |
30 |
Correct |
1 ms |
2188 KB |
Output is correct |
31 |
Correct |
2 ms |
2188 KB |
Output is correct |
32 |
Correct |
1 ms |
2188 KB |
Output is correct |
33 |
Correct |
5 ms |
2576 KB |
Output is correct |
34 |
Correct |
4 ms |
2576 KB |
Output is correct |
35 |
Correct |
10 ms |
2188 KB |
Output is correct |
36 |
Correct |
0 ms |
2188 KB |
Output is correct |
37 |
Correct |
9 ms |
2188 KB |
Output is correct |
38 |
Correct |
10 ms |
2188 KB |
Output is correct |
39 |
Correct |
14 ms |
2188 KB |
Output is correct |
40 |
Correct |
13 ms |
2188 KB |
Output is correct |
41 |
Correct |
12 ms |
2188 KB |
Output is correct |
42 |
Correct |
63 ms |
2188 KB |
Output is correct |
43 |
Correct |
63 ms |
2188 KB |
Output is correct |
44 |
Correct |
66 ms |
2188 KB |
Output is correct |
45 |
Correct |
16 ms |
2576 KB |
Output is correct |
46 |
Correct |
14 ms |
2576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2188 KB |
Output is correct |
2 |
Correct |
0 ms |
2188 KB |
Output is correct |
3 |
Correct |
0 ms |
2188 KB |
Output is correct |
4 |
Correct |
0 ms |
2188 KB |
Output is correct |
5 |
Correct |
0 ms |
2188 KB |
Output is correct |
6 |
Correct |
0 ms |
2188 KB |
Output is correct |
7 |
Correct |
0 ms |
2188 KB |
Output is correct |
8 |
Correct |
0 ms |
2188 KB |
Output is correct |
9 |
Correct |
0 ms |
2188 KB |
Output is correct |
10 |
Correct |
0 ms |
2188 KB |
Output is correct |
11 |
Correct |
0 ms |
2188 KB |
Output is correct |
12 |
Correct |
1 ms |
2188 KB |
Output is correct |
13 |
Correct |
0 ms |
2188 KB |
Output is correct |
14 |
Correct |
0 ms |
2188 KB |
Output is correct |
15 |
Correct |
0 ms |
2188 KB |
Output is correct |
16 |
Correct |
0 ms |
2188 KB |
Output is correct |
17 |
Correct |
0 ms |
2188 KB |
Output is correct |
18 |
Correct |
0 ms |
2188 KB |
Output is correct |
19 |
Correct |
0 ms |
2188 KB |
Output is correct |
20 |
Correct |
2 ms |
2188 KB |
Output is correct |
21 |
Correct |
0 ms |
2188 KB |
Output is correct |
22 |
Correct |
0 ms |
2188 KB |
Output is correct |
23 |
Correct |
0 ms |
2188 KB |
Output is correct |
24 |
Correct |
0 ms |
2188 KB |
Output is correct |
25 |
Correct |
0 ms |
2188 KB |
Output is correct |
26 |
Correct |
4 ms |
2188 KB |
Output is correct |
27 |
Correct |
4 ms |
2188 KB |
Output is correct |
28 |
Correct |
0 ms |
2188 KB |
Output is correct |
29 |
Correct |
0 ms |
2320 KB |
Output is correct |
30 |
Correct |
0 ms |
2188 KB |
Output is correct |
31 |
Correct |
0 ms |
2188 KB |
Output is correct |
32 |
Correct |
0 ms |
2188 KB |
Output is correct |
33 |
Correct |
5 ms |
2576 KB |
Output is correct |
34 |
Correct |
5 ms |
2576 KB |
Output is correct |
35 |
Correct |
7 ms |
2188 KB |
Output is correct |
36 |
Correct |
0 ms |
2188 KB |
Output is correct |
37 |
Correct |
9 ms |
2188 KB |
Output is correct |
38 |
Correct |
10 ms |
2188 KB |
Output is correct |
39 |
Correct |
14 ms |
2188 KB |
Output is correct |
40 |
Correct |
9 ms |
2188 KB |
Output is correct |
41 |
Correct |
13 ms |
2188 KB |
Output is correct |
42 |
Correct |
66 ms |
2188 KB |
Output is correct |
43 |
Correct |
66 ms |
2188 KB |
Output is correct |
44 |
Correct |
67 ms |
2188 KB |
Output is correct |
45 |
Correct |
11 ms |
2576 KB |
Output is correct |
46 |
Correct |
12 ms |
2576 KB |
Output is correct |
47 |
Correct |
22 ms |
2960 KB |
Output is correct |
48 |
Correct |
8 ms |
2188 KB |
Output is correct |
49 |
Correct |
8 ms |
2188 KB |
Output is correct |
50 |
Correct |
6 ms |
2188 KB |
Output is correct |
51 |
Correct |
29 ms |
3728 KB |
Output is correct |
52 |
Correct |
33 ms |
2960 KB |
Output is correct |
53 |
Correct |
10 ms |
2960 KB |
Output is correct |
54 |
Correct |
0 ms |
2320 KB |
Output is correct |
55 |
Correct |
0 ms |
2320 KB |
Output is correct |
56 |
Correct |
882 ms |
2576 KB |
Output is correct |
57 |
Correct |
51 ms |
5264 KB |
Output is correct |
58 |
Correct |
897 ms |
2320 KB |
Output is correct |
59 |
Correct |
853 ms |
2320 KB |
Output is correct |
60 |
Correct |
880 ms |
2320 KB |
Output is correct |
61 |
Correct |
776 ms |
2320 KB |
Output is correct |
62 |
Correct |
12 ms |
2188 KB |
Output is correct |
63 |
Correct |
40 ms |
5264 KB |
Output is correct |
64 |
Correct |
43 ms |
5264 KB |
Output is correct |
65 |
Correct |
55 ms |
5264 KB |
Output is correct |
66 |
Correct |
113 ms |
8336 KB |
Output is correct |
67 |
Correct |
118 ms |
8336 KB |
Output is correct |