#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <time.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef double lf;
const int N_ = 30050;
const int M_ = 30050;
vector<int> jumps[N_];
int N, M;
int target, ans;
int start;
int dst[N_];
bool vis[N_];
void solve1() {
for(int i = 0; i < N; i++) dst[i] = (int)1e9;
dst[start] = 0;
for(int rep = 0; rep < N; rep++) {
int u = -1, d = (int)1e9;
for(int i = 0; i < N; i++) {
if(dst[i] < d && !vis[i]) u = i, d = dst[i];
}
if(u == -1) {
ans = -1;
return;
}
if(u == target) {
ans = d;
return;
}
vis[u] = true;
for(int e = 0; e < jumps[u].size(); e++) {
int p = jumps[u][e];
for(int i = u + p, j = 1; i < N; i += p, j++) {
if(!vis[i] && dst[i] > dst[u] + j) dst[i] = dst[u] + j;
}
for(int i = u - p, j = 1; i >= 0; i -= p, j++) {
if(!vis[i] && dst[i] > dst[u] + j) dst[i] = dst[u] + j;
}
}
}
}
void solve2() {
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
scanf("%d%d", &N, &M);
for(int i = 0; i < M; i++) {
int b, p;
scanf("%d%d", &b, &p);
if(i == 0) start = b;
if(i == 1) target = b;
if(p < N) jumps[b].push_back(p);
}
if(N <= 2000) {
solve1();
}else{
solve2();
}
printf("%d\n", ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2572 KB |
Output is correct |
2 |
Correct |
0 ms |
2572 KB |
Output is correct |
3 |
Correct |
0 ms |
2572 KB |
Output is correct |
4 |
Correct |
0 ms |
2572 KB |
Output is correct |
5 |
Correct |
0 ms |
2572 KB |
Output is correct |
6 |
Correct |
0 ms |
2572 KB |
Output is correct |
7 |
Correct |
0 ms |
2572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2572 KB |
Output is correct |
2 |
Correct |
0 ms |
2572 KB |
Output is correct |
3 |
Correct |
0 ms |
2572 KB |
Output is correct |
4 |
Correct |
1 ms |
2572 KB |
Output is correct |
5 |
Correct |
0 ms |
2572 KB |
Output is correct |
6 |
Correct |
0 ms |
2572 KB |
Output is correct |
7 |
Correct |
0 ms |
2572 KB |
Output is correct |
8 |
Correct |
0 ms |
2572 KB |
Output is correct |
9 |
Correct |
0 ms |
2572 KB |
Output is correct |
10 |
Correct |
0 ms |
2572 KB |
Output is correct |
11 |
Correct |
0 ms |
2572 KB |
Output is correct |
12 |
Correct |
0 ms |
2572 KB |
Output is correct |
13 |
Correct |
0 ms |
2572 KB |
Output is correct |
14 |
Correct |
0 ms |
2572 KB |
Output is correct |
15 |
Correct |
0 ms |
2572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2572 KB |
Output is correct |
2 |
Correct |
0 ms |
2572 KB |
Output is correct |
3 |
Correct |
0 ms |
2572 KB |
Output is correct |
4 |
Correct |
0 ms |
2572 KB |
Output is correct |
5 |
Correct |
0 ms |
2572 KB |
Output is correct |
6 |
Correct |
0 ms |
2572 KB |
Output is correct |
7 |
Correct |
0 ms |
2572 KB |
Output is correct |
8 |
Correct |
0 ms |
2572 KB |
Output is correct |
9 |
Correct |
0 ms |
2572 KB |
Output is correct |
10 |
Correct |
0 ms |
2572 KB |
Output is correct |
11 |
Correct |
1 ms |
2572 KB |
Output is correct |
12 |
Correct |
0 ms |
2572 KB |
Output is correct |
13 |
Correct |
0 ms |
2572 KB |
Output is correct |
14 |
Correct |
0 ms |
2572 KB |
Output is correct |
15 |
Correct |
0 ms |
2572 KB |
Output is correct |
16 |
Correct |
0 ms |
2572 KB |
Output is correct |
17 |
Correct |
0 ms |
2572 KB |
Output is correct |
18 |
Correct |
0 ms |
2572 KB |
Output is correct |
19 |
Correct |
0 ms |
2572 KB |
Output is correct |
20 |
Correct |
11 ms |
2572 KB |
Output is correct |
21 |
Correct |
0 ms |
2572 KB |
Output is correct |
22 |
Correct |
0 ms |
2572 KB |
Output is correct |
23 |
Correct |
7 ms |
2572 KB |
Output is correct |
24 |
Correct |
9 ms |
2572 KB |
Output is correct |
25 |
Correct |
0 ms |
2572 KB |
Output is correct |
26 |
Correct |
5 ms |
2572 KB |
Output is correct |
27 |
Correct |
6 ms |
2572 KB |
Output is correct |
28 |
Correct |
8 ms |
2572 KB |
Output is correct |
29 |
Correct |
6 ms |
2572 KB |
Output is correct |
30 |
Correct |
6 ms |
2572 KB |
Output is correct |
31 |
Correct |
6 ms |
2572 KB |
Output is correct |
32 |
Correct |
6 ms |
2572 KB |
Output is correct |
33 |
Correct |
6 ms |
2572 KB |
Output is correct |
34 |
Correct |
7 ms |
2572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2572 KB |
Output is correct |
2 |
Correct |
0 ms |
2572 KB |
Output is correct |
3 |
Correct |
0 ms |
2572 KB |
Output is correct |
4 |
Correct |
0 ms |
2572 KB |
Output is correct |
5 |
Correct |
0 ms |
2572 KB |
Output is correct |
6 |
Correct |
0 ms |
2572 KB |
Output is correct |
7 |
Correct |
0 ms |
2572 KB |
Output is correct |
8 |
Correct |
0 ms |
2572 KB |
Output is correct |
9 |
Correct |
0 ms |
2572 KB |
Output is correct |
10 |
Correct |
0 ms |
2572 KB |
Output is correct |
11 |
Correct |
0 ms |
2572 KB |
Output is correct |
12 |
Correct |
0 ms |
2572 KB |
Output is correct |
13 |
Correct |
0 ms |
2572 KB |
Output is correct |
14 |
Correct |
0 ms |
2572 KB |
Output is correct |
15 |
Correct |
0 ms |
2572 KB |
Output is correct |
16 |
Correct |
0 ms |
2572 KB |
Output is correct |
17 |
Correct |
0 ms |
2572 KB |
Output is correct |
18 |
Correct |
0 ms |
2572 KB |
Output is correct |
19 |
Correct |
0 ms |
2572 KB |
Output is correct |
20 |
Correct |
11 ms |
2572 KB |
Output is correct |
21 |
Correct |
0 ms |
2572 KB |
Output is correct |
22 |
Correct |
0 ms |
2572 KB |
Output is correct |
23 |
Correct |
6 ms |
2572 KB |
Output is correct |
24 |
Correct |
6 ms |
2572 KB |
Output is correct |
25 |
Correct |
3 ms |
2572 KB |
Output is correct |
26 |
Correct |
5 ms |
2572 KB |
Output is correct |
27 |
Correct |
6 ms |
2572 KB |
Output is correct |
28 |
Correct |
8 ms |
2572 KB |
Output is correct |
29 |
Correct |
3 ms |
2572 KB |
Output is correct |
30 |
Correct |
6 ms |
2572 KB |
Output is correct |
31 |
Correct |
6 ms |
2572 KB |
Output is correct |
32 |
Correct |
6 ms |
2572 KB |
Output is correct |
33 |
Correct |
7 ms |
2572 KB |
Output is correct |
34 |
Correct |
7 ms |
2572 KB |
Output is correct |
35 |
Correct |
14 ms |
2704 KB |
Output is correct |
36 |
Correct |
0 ms |
2572 KB |
Output is correct |
37 |
Correct |
12 ms |
2704 KB |
Output is correct |
38 |
Correct |
0 ms |
2704 KB |
Output is correct |
39 |
Correct |
0 ms |
2704 KB |
Output is correct |
40 |
Correct |
0 ms |
2704 KB |
Output is correct |
41 |
Correct |
0 ms |
2704 KB |
Output is correct |
42 |
Correct |
6 ms |
2704 KB |
Output is correct |
43 |
Correct |
12 ms |
2704 KB |
Output is correct |
44 |
Correct |
71 ms |
2704 KB |
Output is correct |
45 |
Correct |
16 ms |
2704 KB |
Output is correct |
46 |
Correct |
10 ms |
2704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2572 KB |
Output is correct |
2 |
Correct |
0 ms |
2572 KB |
Output is correct |
3 |
Correct |
0 ms |
2572 KB |
Output is correct |
4 |
Correct |
0 ms |
2572 KB |
Output is correct |
5 |
Correct |
0 ms |
2572 KB |
Output is correct |
6 |
Correct |
0 ms |
2572 KB |
Output is correct |
7 |
Correct |
0 ms |
2572 KB |
Output is correct |
8 |
Correct |
0 ms |
2572 KB |
Output is correct |
9 |
Correct |
0 ms |
2572 KB |
Output is correct |
10 |
Correct |
0 ms |
2572 KB |
Output is correct |
11 |
Correct |
0 ms |
2572 KB |
Output is correct |
12 |
Correct |
0 ms |
2572 KB |
Output is correct |
13 |
Correct |
1 ms |
2572 KB |
Output is correct |
14 |
Correct |
1 ms |
2572 KB |
Output is correct |
15 |
Correct |
0 ms |
2572 KB |
Output is correct |
16 |
Correct |
0 ms |
2572 KB |
Output is correct |
17 |
Correct |
0 ms |
2572 KB |
Output is correct |
18 |
Correct |
0 ms |
2572 KB |
Output is correct |
19 |
Correct |
0 ms |
2572 KB |
Output is correct |
20 |
Correct |
11 ms |
2572 KB |
Output is correct |
21 |
Correct |
0 ms |
2572 KB |
Output is correct |
22 |
Correct |
0 ms |
2572 KB |
Output is correct |
23 |
Correct |
6 ms |
2572 KB |
Output is correct |
24 |
Correct |
9 ms |
2572 KB |
Output is correct |
25 |
Correct |
0 ms |
2572 KB |
Output is correct |
26 |
Correct |
5 ms |
2572 KB |
Output is correct |
27 |
Correct |
6 ms |
2572 KB |
Output is correct |
28 |
Correct |
6 ms |
2572 KB |
Output is correct |
29 |
Correct |
3 ms |
2572 KB |
Output is correct |
30 |
Correct |
6 ms |
2572 KB |
Output is correct |
31 |
Correct |
6 ms |
2572 KB |
Output is correct |
32 |
Correct |
6 ms |
2572 KB |
Output is correct |
33 |
Correct |
6 ms |
2572 KB |
Output is correct |
34 |
Correct |
6 ms |
2572 KB |
Output is correct |
35 |
Correct |
12 ms |
2704 KB |
Output is correct |
36 |
Correct |
0 ms |
2572 KB |
Output is correct |
37 |
Correct |
12 ms |
2704 KB |
Output is correct |
38 |
Correct |
9 ms |
2704 KB |
Output is correct |
39 |
Correct |
9 ms |
2704 KB |
Output is correct |
40 |
Correct |
11 ms |
2704 KB |
Output is correct |
41 |
Correct |
0 ms |
2704 KB |
Output is correct |
42 |
Correct |
13 ms |
2704 KB |
Output is correct |
43 |
Correct |
8 ms |
2704 KB |
Output is correct |
44 |
Correct |
71 ms |
2704 KB |
Output is correct |
45 |
Correct |
8 ms |
2704 KB |
Output is correct |
46 |
Correct |
15 ms |
2704 KB |
Output is correct |
47 |
Incorrect |
7 ms |
2836 KB |
Output isn't correct |
48 |
Halted |
0 ms |
0 KB |
- |