#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];
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)chk[p[i][0]][p[i][1]] = 1;
}
for (int i = 0; i < m; i++){
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;
}
}
for (int i = 0; i < m; i++){
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;
}
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Correct |
0 ms |
23028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Correct |
0 ms |
23028 KB |
Output is correct |
8 |
Correct |
0 ms |
23028 KB |
Output is correct |
9 |
Correct |
0 ms |
23028 KB |
Output is correct |
10 |
Correct |
0 ms |
23028 KB |
Output is correct |
11 |
Correct |
0 ms |
23028 KB |
Output is correct |
12 |
Correct |
0 ms |
26108 KB |
Output is correct |
13 |
Correct |
0 ms |
23028 KB |
Output is correct |
14 |
Correct |
1 ms |
23028 KB |
Output is correct |
15 |
Correct |
0 ms |
23028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Correct |
0 ms |
23028 KB |
Output is correct |
8 |
Correct |
0 ms |
23028 KB |
Output is correct |
9 |
Correct |
0 ms |
23028 KB |
Output is correct |
10 |
Correct |
0 ms |
23028 KB |
Output is correct |
11 |
Correct |
1 ms |
23028 KB |
Output is correct |
12 |
Correct |
0 ms |
26108 KB |
Output is correct |
13 |
Correct |
0 ms |
23028 KB |
Output is correct |
14 |
Correct |
0 ms |
23028 KB |
Output is correct |
15 |
Correct |
0 ms |
23028 KB |
Output is correct |
16 |
Correct |
1 ms |
23028 KB |
Output is correct |
17 |
Correct |
0 ms |
23160 KB |
Output is correct |
18 |
Correct |
0 ms |
23028 KB |
Output is correct |
19 |
Correct |
0 ms |
23028 KB |
Output is correct |
20 |
Correct |
0 ms |
23028 KB |
Output is correct |
21 |
Correct |
0 ms |
23028 KB |
Output is correct |
22 |
Correct |
0 ms |
23028 KB |
Output is correct |
23 |
Correct |
0 ms |
23028 KB |
Output is correct |
24 |
Correct |
0 ms |
23160 KB |
Output is correct |
25 |
Correct |
0 ms |
23028 KB |
Output is correct |
26 |
Correct |
49 ms |
72220 KB |
Output is correct |
27 |
Correct |
49 ms |
72212 KB |
Output is correct |
28 |
Correct |
2 ms |
23160 KB |
Output is correct |
29 |
Correct |
2 ms |
23952 KB |
Output is correct |
30 |
Correct |
0 ms |
23300 KB |
Output is correct |
31 |
Correct |
0 ms |
23456 KB |
Output is correct |
32 |
Correct |
2 ms |
23292 KB |
Output is correct |
33 |
Correct |
6 ms |
24712 KB |
Output is correct |
34 |
Correct |
3 ms |
24616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Correct |
0 ms |
23028 KB |
Output is correct |
8 |
Correct |
0 ms |
23028 KB |
Output is correct |
9 |
Correct |
0 ms |
23028 KB |
Output is correct |
10 |
Correct |
0 ms |
23028 KB |
Output is correct |
11 |
Correct |
0 ms |
23028 KB |
Output is correct |
12 |
Correct |
3 ms |
26108 KB |
Output is correct |
13 |
Correct |
0 ms |
23028 KB |
Output is correct |
14 |
Correct |
0 ms |
23028 KB |
Output is correct |
15 |
Correct |
0 ms |
23028 KB |
Output is correct |
16 |
Correct |
0 ms |
23028 KB |
Output is correct |
17 |
Correct |
0 ms |
23160 KB |
Output is correct |
18 |
Correct |
0 ms |
23028 KB |
Output is correct |
19 |
Correct |
0 ms |
23028 KB |
Output is correct |
20 |
Correct |
0 ms |
23028 KB |
Output is correct |
21 |
Correct |
0 ms |
23028 KB |
Output is correct |
22 |
Correct |
0 ms |
23028 KB |
Output is correct |
23 |
Correct |
0 ms |
23028 KB |
Output is correct |
24 |
Correct |
0 ms |
23160 KB |
Output is correct |
25 |
Correct |
0 ms |
23028 KB |
Output is correct |
26 |
Correct |
56 ms |
72220 KB |
Output is correct |
27 |
Correct |
45 ms |
72212 KB |
Output is correct |
28 |
Correct |
0 ms |
23160 KB |
Output is correct |
29 |
Correct |
0 ms |
23952 KB |
Output is correct |
30 |
Correct |
2 ms |
23300 KB |
Output is correct |
31 |
Correct |
0 ms |
23456 KB |
Output is correct |
32 |
Correct |
0 ms |
23292 KB |
Output is correct |
33 |
Correct |
7 ms |
24712 KB |
Output is correct |
34 |
Correct |
4 ms |
24616 KB |
Output is correct |
35 |
Correct |
0 ms |
24088 KB |
Output is correct |
36 |
Correct |
0 ms |
23160 KB |
Output is correct |
37 |
Correct |
10 ms |
25140 KB |
Output is correct |
38 |
Correct |
9 ms |
24924 KB |
Output is correct |
39 |
Correct |
12 ms |
24744 KB |
Output is correct |
40 |
Correct |
13 ms |
24744 KB |
Output is correct |
41 |
Correct |
0 ms |
24920 KB |
Output is correct |
42 |
Memory limit exceeded |
238 ms |
262144 KB |
Memory limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Correct |
0 ms |
23028 KB |
Output is correct |
8 |
Correct |
0 ms |
23028 KB |
Output is correct |
9 |
Correct |
0 ms |
23028 KB |
Output is correct |
10 |
Correct |
0 ms |
23028 KB |
Output is correct |
11 |
Correct |
0 ms |
23028 KB |
Output is correct |
12 |
Correct |
0 ms |
26108 KB |
Output is correct |
13 |
Correct |
0 ms |
23028 KB |
Output is correct |
14 |
Correct |
1 ms |
23028 KB |
Output is correct |
15 |
Correct |
0 ms |
23028 KB |
Output is correct |
16 |
Correct |
0 ms |
23028 KB |
Output is correct |
17 |
Correct |
0 ms |
23160 KB |
Output is correct |
18 |
Correct |
0 ms |
23028 KB |
Output is correct |
19 |
Correct |
0 ms |
23028 KB |
Output is correct |
20 |
Correct |
0 ms |
23028 KB |
Output is correct |
21 |
Correct |
0 ms |
23028 KB |
Output is correct |
22 |
Correct |
0 ms |
23028 KB |
Output is correct |
23 |
Correct |
2 ms |
23028 KB |
Output is correct |
24 |
Correct |
3 ms |
23160 KB |
Output is correct |
25 |
Correct |
0 ms |
23028 KB |
Output is correct |
26 |
Correct |
60 ms |
72220 KB |
Output is correct |
27 |
Correct |
38 ms |
72212 KB |
Output is correct |
28 |
Correct |
0 ms |
23160 KB |
Output is correct |
29 |
Correct |
0 ms |
23952 KB |
Output is correct |
30 |
Correct |
0 ms |
23300 KB |
Output is correct |
31 |
Correct |
1 ms |
23456 KB |
Output is correct |
32 |
Correct |
1 ms |
23292 KB |
Output is correct |
33 |
Correct |
6 ms |
24712 KB |
Output is correct |
34 |
Correct |
3 ms |
24616 KB |
Output is correct |
35 |
Correct |
9 ms |
24088 KB |
Output is correct |
36 |
Correct |
0 ms |
23160 KB |
Output is correct |
37 |
Correct |
13 ms |
25140 KB |
Output is correct |
38 |
Correct |
15 ms |
24924 KB |
Output is correct |
39 |
Correct |
14 ms |
24744 KB |
Output is correct |
40 |
Correct |
9 ms |
24744 KB |
Output is correct |
41 |
Correct |
12 ms |
24920 KB |
Output is correct |
42 |
Memory limit exceeded |
233 ms |
262144 KB |
Memory limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |