#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> Pi;
const int MX = 30003;
const int SX = 170;
priority_queue <Pi, vector<Pi>, greater<Pi> > pq;
vector <Pi> E[MX];
int n, m;
int p[MX][2];
int chk[MX][SX + 4];
int len[MX], out[MX];
int main(){
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++){
scanf("%d%d", p[i], p[i] + 1);
if (p[i][1] < SX){
if (!chk[p[i][0]][p[i][1]])chk[p[i][0]][p[i][1]] = 1;
else out[i] = 1;
}
}
for (int i = 0; i < m; i++){
if (out[i])continue;
for (int j = p[i][0] - p[i][1], l = 1; j >= 0; j -= p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
for (int j = p[i][0] + p[i][1], l = 1; j < n; j += p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
}
pq.push(Pi(0, p[0][0]));
for (int i = 0; i < n; i++)len[i] = (int)1e9;
len[p[0][0]] = 0;
while (!pq.empty()){
Pi t = pq.top();
pq.pop();
if (len[t.Y] != t.X)continue;
if (t.Y == p[1][0])break;
for (int i = 0; i < (int)E[t.Y].size(); i++){
Pi &w = E[t.Y][i];
if (len[w.X] > len[t.Y] + w.Y){
len[w.X] = len[t.Y] + w.Y;
pq.push(Pi(len[w.X], w.X));
}
}
}
printf("%d", len[p[1][0]] == (int)1e9 ? -1 : len[p[1][0]]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23144 KB |
Output is correct |
2 |
Correct |
0 ms |
23144 KB |
Output is correct |
3 |
Correct |
0 ms |
23144 KB |
Output is correct |
4 |
Correct |
0 ms |
23144 KB |
Output is correct |
5 |
Correct |
0 ms |
23144 KB |
Output is correct |
6 |
Correct |
0 ms |
23144 KB |
Output is correct |
7 |
Correct |
0 ms |
23144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23144 KB |
Output is correct |
2 |
Correct |
0 ms |
23144 KB |
Output is correct |
3 |
Correct |
0 ms |
23144 KB |
Output is correct |
4 |
Correct |
0 ms |
23144 KB |
Output is correct |
5 |
Correct |
0 ms |
23144 KB |
Output is correct |
6 |
Correct |
0 ms |
23144 KB |
Output is correct |
7 |
Correct |
0 ms |
23144 KB |
Output is correct |
8 |
Correct |
0 ms |
23144 KB |
Output is correct |
9 |
Correct |
0 ms |
23144 KB |
Output is correct |
10 |
Correct |
0 ms |
23144 KB |
Output is correct |
11 |
Correct |
0 ms |
23144 KB |
Output is correct |
12 |
Correct |
0 ms |
23144 KB |
Output is correct |
13 |
Correct |
0 ms |
23144 KB |
Output is correct |
14 |
Correct |
1 ms |
23144 KB |
Output is correct |
15 |
Correct |
0 ms |
23144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23144 KB |
Output is correct |
2 |
Correct |
0 ms |
23144 KB |
Output is correct |
3 |
Correct |
0 ms |
23144 KB |
Output is correct |
4 |
Correct |
0 ms |
23144 KB |
Output is correct |
5 |
Correct |
0 ms |
23144 KB |
Output is correct |
6 |
Correct |
0 ms |
23144 KB |
Output is correct |
7 |
Correct |
0 ms |
23144 KB |
Output is correct |
8 |
Correct |
0 ms |
23144 KB |
Output is correct |
9 |
Correct |
0 ms |
23144 KB |
Output is correct |
10 |
Correct |
1 ms |
23144 KB |
Output is correct |
11 |
Correct |
0 ms |
23144 KB |
Output is correct |
12 |
Correct |
0 ms |
23144 KB |
Output is correct |
13 |
Correct |
0 ms |
23144 KB |
Output is correct |
14 |
Correct |
1 ms |
23144 KB |
Output is correct |
15 |
Correct |
0 ms |
23144 KB |
Output is correct |
16 |
Correct |
0 ms |
23144 KB |
Output is correct |
17 |
Correct |
0 ms |
23276 KB |
Output is correct |
18 |
Correct |
0 ms |
23144 KB |
Output is correct |
19 |
Correct |
0 ms |
23144 KB |
Output is correct |
20 |
Correct |
0 ms |
23144 KB |
Output is correct |
21 |
Correct |
0 ms |
23144 KB |
Output is correct |
22 |
Correct |
0 ms |
23144 KB |
Output is correct |
23 |
Correct |
2 ms |
23144 KB |
Output is correct |
24 |
Correct |
0 ms |
23276 KB |
Output is correct |
25 |
Correct |
0 ms |
23144 KB |
Output is correct |
26 |
Correct |
0 ms |
23288 KB |
Output is correct |
27 |
Correct |
0 ms |
23144 KB |
Output is correct |
28 |
Correct |
3 ms |
23276 KB |
Output is correct |
29 |
Correct |
0 ms |
24060 KB |
Output is correct |
30 |
Correct |
0 ms |
23408 KB |
Output is correct |
31 |
Correct |
2 ms |
23572 KB |
Output is correct |
32 |
Correct |
3 ms |
23416 KB |
Output is correct |
33 |
Correct |
7 ms |
24800 KB |
Output is correct |
34 |
Correct |
4 ms |
24772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23144 KB |
Output is correct |
2 |
Correct |
0 ms |
23144 KB |
Output is correct |
3 |
Correct |
0 ms |
23144 KB |
Output is correct |
4 |
Correct |
0 ms |
23144 KB |
Output is correct |
5 |
Correct |
0 ms |
23144 KB |
Output is correct |
6 |
Correct |
0 ms |
23144 KB |
Output is correct |
7 |
Correct |
0 ms |
23144 KB |
Output is correct |
8 |
Correct |
0 ms |
23144 KB |
Output is correct |
9 |
Correct |
0 ms |
23144 KB |
Output is correct |
10 |
Correct |
0 ms |
23144 KB |
Output is correct |
11 |
Correct |
0 ms |
23144 KB |
Output is correct |
12 |
Correct |
0 ms |
23144 KB |
Output is correct |
13 |
Correct |
1 ms |
23144 KB |
Output is correct |
14 |
Correct |
0 ms |
23144 KB |
Output is correct |
15 |
Correct |
0 ms |
23144 KB |
Output is correct |
16 |
Correct |
0 ms |
23144 KB |
Output is correct |
17 |
Correct |
0 ms |
23276 KB |
Output is correct |
18 |
Correct |
0 ms |
23144 KB |
Output is correct |
19 |
Correct |
0 ms |
23144 KB |
Output is correct |
20 |
Correct |
0 ms |
23144 KB |
Output is correct |
21 |
Correct |
0 ms |
23144 KB |
Output is correct |
22 |
Correct |
0 ms |
23144 KB |
Output is correct |
23 |
Correct |
0 ms |
23144 KB |
Output is correct |
24 |
Correct |
0 ms |
23276 KB |
Output is correct |
25 |
Correct |
0 ms |
23144 KB |
Output is correct |
26 |
Correct |
0 ms |
23288 KB |
Output is correct |
27 |
Correct |
0 ms |
23144 KB |
Output is correct |
28 |
Correct |
2 ms |
23276 KB |
Output is correct |
29 |
Correct |
0 ms |
24060 KB |
Output is correct |
30 |
Correct |
2 ms |
23408 KB |
Output is correct |
31 |
Correct |
0 ms |
23572 KB |
Output is correct |
32 |
Correct |
3 ms |
23416 KB |
Output is correct |
33 |
Correct |
7 ms |
24800 KB |
Output is correct |
34 |
Correct |
7 ms |
24772 KB |
Output is correct |
35 |
Correct |
6 ms |
24200 KB |
Output is correct |
36 |
Correct |
0 ms |
23276 KB |
Output is correct |
37 |
Correct |
11 ms |
25272 KB |
Output is correct |
38 |
Correct |
12 ms |
25040 KB |
Output is correct |
39 |
Correct |
8 ms |
24860 KB |
Output is correct |
40 |
Correct |
8 ms |
24860 KB |
Output is correct |
41 |
Correct |
0 ms |
25040 KB |
Output is correct |
42 |
Correct |
0 ms |
23288 KB |
Output is correct |
43 |
Correct |
10 ms |
23144 KB |
Output is correct |
44 |
Correct |
8 ms |
23144 KB |
Output is correct |
45 |
Correct |
12 ms |
28672 KB |
Output is correct |
46 |
Correct |
14 ms |
28668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
23144 KB |
Output is correct |
2 |
Correct |
0 ms |
23144 KB |
Output is correct |
3 |
Correct |
0 ms |
23144 KB |
Output is correct |
4 |
Correct |
0 ms |
23144 KB |
Output is correct |
5 |
Correct |
0 ms |
23144 KB |
Output is correct |
6 |
Correct |
0 ms |
23144 KB |
Output is correct |
7 |
Correct |
0 ms |
23144 KB |
Output is correct |
8 |
Correct |
0 ms |
23144 KB |
Output is correct |
9 |
Correct |
0 ms |
23144 KB |
Output is correct |
10 |
Correct |
1 ms |
23144 KB |
Output is correct |
11 |
Correct |
0 ms |
23144 KB |
Output is correct |
12 |
Correct |
0 ms |
23144 KB |
Output is correct |
13 |
Correct |
0 ms |
23144 KB |
Output is correct |
14 |
Correct |
0 ms |
23144 KB |
Output is correct |
15 |
Correct |
0 ms |
23144 KB |
Output is correct |
16 |
Correct |
0 ms |
23144 KB |
Output is correct |
17 |
Correct |
0 ms |
23276 KB |
Output is correct |
18 |
Correct |
0 ms |
23144 KB |
Output is correct |
19 |
Correct |
1 ms |
23144 KB |
Output is correct |
20 |
Correct |
0 ms |
23144 KB |
Output is correct |
21 |
Correct |
0 ms |
23144 KB |
Output is correct |
22 |
Correct |
0 ms |
23144 KB |
Output is correct |
23 |
Correct |
2 ms |
23144 KB |
Output is correct |
24 |
Correct |
2 ms |
23276 KB |
Output is correct |
25 |
Correct |
0 ms |
23144 KB |
Output is correct |
26 |
Correct |
0 ms |
23288 KB |
Output is correct |
27 |
Correct |
2 ms |
23144 KB |
Output is correct |
28 |
Correct |
0 ms |
23276 KB |
Output is correct |
29 |
Correct |
0 ms |
24060 KB |
Output is correct |
30 |
Correct |
2 ms |
23408 KB |
Output is correct |
31 |
Correct |
0 ms |
23572 KB |
Output is correct |
32 |
Correct |
0 ms |
23416 KB |
Output is correct |
33 |
Correct |
4 ms |
24800 KB |
Output is correct |
34 |
Correct |
6 ms |
24772 KB |
Output is correct |
35 |
Correct |
12 ms |
24200 KB |
Output is correct |
36 |
Correct |
0 ms |
23276 KB |
Output is correct |
37 |
Correct |
10 ms |
25272 KB |
Output is correct |
38 |
Correct |
17 ms |
25040 KB |
Output is correct |
39 |
Correct |
12 ms |
24860 KB |
Output is correct |
40 |
Correct |
14 ms |
24860 KB |
Output is correct |
41 |
Correct |
11 ms |
25040 KB |
Output is correct |
42 |
Correct |
0 ms |
23288 KB |
Output is correct |
43 |
Correct |
0 ms |
23144 KB |
Output is correct |
44 |
Correct |
0 ms |
23144 KB |
Output is correct |
45 |
Correct |
24 ms |
28672 KB |
Output is correct |
46 |
Correct |
7 ms |
28668 KB |
Output is correct |
47 |
Correct |
29 ms |
32828 KB |
Output is correct |
48 |
Correct |
17 ms |
25648 KB |
Output is correct |
49 |
Correct |
10 ms |
24636 KB |
Output is correct |
50 |
Correct |
0 ms |
24648 KB |
Output is correct |
51 |
Correct |
28 ms |
27636 KB |
Output is correct |
52 |
Correct |
34 ms |
27960 KB |
Output is correct |
53 |
Correct |
14 ms |
24300 KB |
Output is correct |
54 |
Correct |
2 ms |
23540 KB |
Output is correct |
55 |
Correct |
2 ms |
24052 KB |
Output is correct |
56 |
Correct |
15 ms |
24068 KB |
Output is correct |
57 |
Correct |
20 ms |
31952 KB |
Output is correct |
58 |
Correct |
8 ms |
24564 KB |
Output is correct |
59 |
Correct |
10 ms |
24676 KB |
Output is correct |
60 |
Correct |
8 ms |
25344 KB |
Output is correct |
61 |
Correct |
8 ms |
24368 KB |
Output is correct |
62 |
Correct |
20 ms |
28308 KB |
Output is correct |
63 |
Correct |
85 ms |
51100 KB |
Output is correct |
64 |
Correct |
79 ms |
63920 KB |
Output is correct |
65 |
Correct |
129 ms |
80020 KB |
Output is correct |
66 |
Correct |
216 ms |
112340 KB |
Output is correct |
67 |
Correct |
178 ms |
112028 KB |
Output is correct |