#include<stdio.h>
#include<algorithm>
#include<vector>
#include<map>
#define N_ 7600000
using namespace std;
int n, m, Num[N_], D[N_], Q1[N_], Q2[N_];
vector<int>E[30100];
map<int, int>Map;
bool be[N_], ed[N_];
struct A{
int a, b;
}w[30100];
int cnt;
void Make(int a, int b, int x){
int i;
be[cnt + 1] = true;
for (i = b; i < n; i += a){
++cnt;
Num[cnt] = i;
if (x == i) E[x].push_back(cnt);
}
ed[cnt] = true;
}
void BFS(int a){
int i, h = 0, t = 0, tt = 0, x;
for (i = 0; i <= cnt; i++)D[i] = 999999999;
D[a] = 0;
Q1[++t] = a;
while (t){
while (h < t){
x = Q1[++h];
if (x<n){
for (i = 0; i<E[x].size(); i++){
if (D[E[x][i]] > D[x]){
D[E[x][i]] = D[x];
Q1[++t] = E[x][i];
}
}
}
else{
if (D[Num[x]] > D[x]){
D[Num[x]] = D[x];
Q1[++t] = Num[x];
}
}
}
tt = 0;
for (i = 1; i <= t; i++){
if (Q1[i]<n)continue;
x = Q1[i];
if (!be[x] && D[x - 1] >D[x] + 1){
D[x - 1] = D[x] + 1;
Q2[++tt] = x - 1;
}
if (!ed[x] && D[x + 1] > D[x] + 1){
D[x + 1] = D[x] + 1;
Q2[++tt] = x + 1;
}
}
for (i = 1; i <= tt; i++)Q1[i] = Q2[i];
t = tt;
h = 0;
}
}
int main(){
int i, p1, p2, t;
scanf("%d%d", &n, &m);
cnt = n;
for (i = 1; i <= m; i++){
scanf("%d%d", &w[i].a, &w[i].b);
t = w[i].b * 30100 + (w[i].a%w[i].b);
if (!Map[t]){
Map[t] = cnt + 1;
Make(w[i].b, w[i].a%w[i].b, w[i].a);
}
else{
E[w[i].a].push_back(Map[t] + w[i].a / w[i].b);
}
}
p1 = w[1].a, p2 = w[2].a;
BFS(p1);
printf("%d\n", D[p2] == 999999999 ? -1 : D[p2]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
135748 KB |
Output is correct |
2 |
Correct |
0 ms |
135748 KB |
Output is correct |
3 |
Correct |
0 ms |
135748 KB |
Output is correct |
4 |
Correct |
0 ms |
135748 KB |
Output is correct |
5 |
Correct |
0 ms |
135748 KB |
Output is correct |
6 |
Correct |
0 ms |
135748 KB |
Output is correct |
7 |
Correct |
0 ms |
135748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
135748 KB |
Output is correct |
2 |
Correct |
0 ms |
135748 KB |
Output is correct |
3 |
Correct |
0 ms |
135748 KB |
Output is correct |
4 |
Correct |
0 ms |
135748 KB |
Output is correct |
5 |
Correct |
0 ms |
135748 KB |
Output is correct |
6 |
Correct |
0 ms |
135748 KB |
Output is correct |
7 |
Correct |
0 ms |
135748 KB |
Output is correct |
8 |
Correct |
0 ms |
135748 KB |
Output is correct |
9 |
Correct |
0 ms |
135748 KB |
Output is correct |
10 |
Correct |
0 ms |
135748 KB |
Output is correct |
11 |
Correct |
3 ms |
135748 KB |
Output is correct |
12 |
Correct |
0 ms |
135748 KB |
Output is correct |
13 |
Correct |
2 ms |
135748 KB |
Output is correct |
14 |
Correct |
0 ms |
135748 KB |
Output is correct |
15 |
Correct |
0 ms |
135748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
135748 KB |
Output is correct |
2 |
Correct |
0 ms |
135748 KB |
Output is correct |
3 |
Correct |
0 ms |
135748 KB |
Output is correct |
4 |
Correct |
0 ms |
135748 KB |
Output is correct |
5 |
Correct |
0 ms |
135748 KB |
Output is correct |
6 |
Correct |
0 ms |
135748 KB |
Output is correct |
7 |
Correct |
0 ms |
135748 KB |
Output is correct |
8 |
Correct |
0 ms |
135748 KB |
Output is correct |
9 |
Correct |
0 ms |
135748 KB |
Output is correct |
10 |
Correct |
0 ms |
135748 KB |
Output is correct |
11 |
Correct |
0 ms |
135748 KB |
Output is correct |
12 |
Correct |
0 ms |
135748 KB |
Output is correct |
13 |
Correct |
0 ms |
135748 KB |
Output is correct |
14 |
Correct |
0 ms |
135748 KB |
Output is correct |
15 |
Correct |
2 ms |
135748 KB |
Output is correct |
16 |
Correct |
2 ms |
135748 KB |
Output is correct |
17 |
Correct |
0 ms |
135748 KB |
Output is correct |
18 |
Correct |
2 ms |
135748 KB |
Output is correct |
19 |
Correct |
0 ms |
135748 KB |
Output is correct |
20 |
Correct |
0 ms |
135748 KB |
Output is correct |
21 |
Correct |
0 ms |
135748 KB |
Output is correct |
22 |
Correct |
0 ms |
135748 KB |
Output is correct |
23 |
Correct |
0 ms |
135748 KB |
Output is correct |
24 |
Correct |
2 ms |
135748 KB |
Output is correct |
25 |
Correct |
0 ms |
135880 KB |
Output is correct |
26 |
Correct |
0 ms |
135748 KB |
Output is correct |
27 |
Correct |
0 ms |
135748 KB |
Output is correct |
28 |
Correct |
0 ms |
135880 KB |
Output is correct |
29 |
Correct |
0 ms |
135748 KB |
Output is correct |
30 |
Correct |
0 ms |
135748 KB |
Output is correct |
31 |
Correct |
0 ms |
135748 KB |
Output is correct |
32 |
Correct |
0 ms |
135748 KB |
Output is correct |
33 |
Correct |
2 ms |
135748 KB |
Output is correct |
34 |
Correct |
5 ms |
135748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
135748 KB |
Output is correct |
2 |
Correct |
0 ms |
135748 KB |
Output is correct |
3 |
Correct |
0 ms |
135748 KB |
Output is correct |
4 |
Correct |
0 ms |
135748 KB |
Output is correct |
5 |
Correct |
0 ms |
135748 KB |
Output is correct |
6 |
Correct |
0 ms |
135748 KB |
Output is correct |
7 |
Correct |
0 ms |
135748 KB |
Output is correct |
8 |
Correct |
0 ms |
135748 KB |
Output is correct |
9 |
Correct |
0 ms |
135748 KB |
Output is correct |
10 |
Correct |
0 ms |
135748 KB |
Output is correct |
11 |
Correct |
3 ms |
135748 KB |
Output is correct |
12 |
Correct |
0 ms |
135748 KB |
Output is correct |
13 |
Correct |
0 ms |
135748 KB |
Output is correct |
14 |
Correct |
3 ms |
135748 KB |
Output is correct |
15 |
Correct |
0 ms |
135748 KB |
Output is correct |
16 |
Correct |
0 ms |
135748 KB |
Output is correct |
17 |
Correct |
2 ms |
135748 KB |
Output is correct |
18 |
Correct |
0 ms |
135748 KB |
Output is correct |
19 |
Correct |
0 ms |
135748 KB |
Output is correct |
20 |
Correct |
0 ms |
135748 KB |
Output is correct |
21 |
Correct |
0 ms |
135748 KB |
Output is correct |
22 |
Correct |
0 ms |
135748 KB |
Output is correct |
23 |
Correct |
3 ms |
135748 KB |
Output is correct |
24 |
Correct |
0 ms |
135748 KB |
Output is correct |
25 |
Correct |
2 ms |
135880 KB |
Output is correct |
26 |
Correct |
0 ms |
135748 KB |
Output is correct |
27 |
Correct |
0 ms |
135748 KB |
Output is correct |
28 |
Correct |
3 ms |
135880 KB |
Output is correct |
29 |
Correct |
3 ms |
135748 KB |
Output is correct |
30 |
Correct |
0 ms |
135748 KB |
Output is correct |
31 |
Correct |
0 ms |
135748 KB |
Output is correct |
32 |
Correct |
0 ms |
135748 KB |
Output is correct |
33 |
Correct |
2 ms |
135748 KB |
Output is correct |
34 |
Correct |
0 ms |
135748 KB |
Output is correct |
35 |
Correct |
23 ms |
137068 KB |
Output is correct |
36 |
Correct |
2 ms |
136012 KB |
Output is correct |
37 |
Correct |
12 ms |
136672 KB |
Output is correct |
38 |
Correct |
31 ms |
137332 KB |
Output is correct |
39 |
Correct |
22 ms |
137332 KB |
Output is correct |
40 |
Correct |
29 ms |
137332 KB |
Output is correct |
41 |
Correct |
26 ms |
137332 KB |
Output is correct |
42 |
Correct |
4 ms |
135884 KB |
Output is correct |
43 |
Correct |
9 ms |
135892 KB |
Output is correct |
44 |
Correct |
6 ms |
135880 KB |
Output is correct |
45 |
Correct |
30 ms |
137332 KB |
Output is correct |
46 |
Correct |
36 ms |
137332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
135748 KB |
Output is correct |
2 |
Correct |
0 ms |
135748 KB |
Output is correct |
3 |
Correct |
0 ms |
135748 KB |
Output is correct |
4 |
Correct |
0 ms |
135748 KB |
Output is correct |
5 |
Correct |
0 ms |
135748 KB |
Output is correct |
6 |
Correct |
0 ms |
135748 KB |
Output is correct |
7 |
Correct |
0 ms |
135748 KB |
Output is correct |
8 |
Correct |
0 ms |
135748 KB |
Output is correct |
9 |
Correct |
0 ms |
135748 KB |
Output is correct |
10 |
Correct |
0 ms |
135748 KB |
Output is correct |
11 |
Correct |
0 ms |
135748 KB |
Output is correct |
12 |
Correct |
0 ms |
135748 KB |
Output is correct |
13 |
Correct |
0 ms |
135748 KB |
Output is correct |
14 |
Correct |
2 ms |
135748 KB |
Output is correct |
15 |
Correct |
3 ms |
135748 KB |
Output is correct |
16 |
Correct |
0 ms |
135748 KB |
Output is correct |
17 |
Correct |
0 ms |
135748 KB |
Output is correct |
18 |
Correct |
0 ms |
135748 KB |
Output is correct |
19 |
Correct |
0 ms |
135748 KB |
Output is correct |
20 |
Correct |
3 ms |
135748 KB |
Output is correct |
21 |
Correct |
3 ms |
135748 KB |
Output is correct |
22 |
Correct |
2 ms |
135748 KB |
Output is correct |
23 |
Correct |
0 ms |
135748 KB |
Output is correct |
24 |
Correct |
2 ms |
135748 KB |
Output is correct |
25 |
Correct |
0 ms |
135880 KB |
Output is correct |
26 |
Correct |
3 ms |
135748 KB |
Output is correct |
27 |
Correct |
0 ms |
135748 KB |
Output is correct |
28 |
Correct |
2 ms |
135880 KB |
Output is correct |
29 |
Correct |
0 ms |
135748 KB |
Output is correct |
30 |
Correct |
0 ms |
135748 KB |
Output is correct |
31 |
Correct |
0 ms |
135748 KB |
Output is correct |
32 |
Correct |
0 ms |
135748 KB |
Output is correct |
33 |
Correct |
5 ms |
135748 KB |
Output is correct |
34 |
Correct |
0 ms |
135748 KB |
Output is correct |
35 |
Correct |
20 ms |
137068 KB |
Output is correct |
36 |
Correct |
0 ms |
136012 KB |
Output is correct |
37 |
Correct |
22 ms |
136672 KB |
Output is correct |
38 |
Correct |
32 ms |
137332 KB |
Output is correct |
39 |
Correct |
28 ms |
137332 KB |
Output is correct |
40 |
Correct |
28 ms |
137332 KB |
Output is correct |
41 |
Correct |
24 ms |
137332 KB |
Output is correct |
42 |
Correct |
9 ms |
135884 KB |
Output is correct |
43 |
Correct |
9 ms |
135892 KB |
Output is correct |
44 |
Correct |
9 ms |
135880 KB |
Output is correct |
45 |
Correct |
25 ms |
137332 KB |
Output is correct |
46 |
Correct |
28 ms |
137332 KB |
Output is correct |
47 |
Correct |
41 ms |
137200 KB |
Output is correct |
48 |
Correct |
25 ms |
137200 KB |
Output is correct |
49 |
Correct |
22 ms |
137068 KB |
Output is correct |
50 |
Correct |
15 ms |
136804 KB |
Output is correct |
51 |
Correct |
38 ms |
137728 KB |
Output is correct |
52 |
Correct |
34 ms |
137728 KB |
Output is correct |
53 |
Correct |
27 ms |
137728 KB |
Output is correct |
54 |
Correct |
0 ms |
135748 KB |
Output is correct |
55 |
Correct |
2 ms |
135748 KB |
Output is correct |
56 |
Correct |
10 ms |
136672 KB |
Output is correct |
57 |
Correct |
12 ms |
135748 KB |
Output is correct |
58 |
Correct |
7 ms |
135880 KB |
Output is correct |
59 |
Correct |
6 ms |
135880 KB |
Output is correct |
60 |
Correct |
0 ms |
135896 KB |
Output is correct |
61 |
Correct |
13 ms |
136040 KB |
Output is correct |
62 |
Correct |
34 ms |
137992 KB |
Output is correct |
63 |
Correct |
71 ms |
137332 KB |
Output is correct |
64 |
Correct |
83 ms |
137332 KB |
Output is correct |
65 |
Correct |
116 ms |
137332 KB |
Output is correct |
66 |
Correct |
193 ms |
137332 KB |
Output is correct |
67 |
Correct |
188 ms |
137332 KB |
Output is correct |