#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 30010;
const int MX = 1e6;
const int inf = (int)1e9;
int dist[N];
vector<int> id[MX];
vector<int> q[N];
int main(){
fastIO;
int n, m;
cin >> n >> m;
int st = -1, en = -1;
int xi, yi;
for(int i = 0 ; i < n; i ++ ){
dist[i] = inf;
}
for(int i = 0 ; i < m; i ++ ){
cin >> xi >> yi;
q[xi].push_back(yi);
if(i == 0){
st = xi;
}
else if(i == 1){
en = xi;
}
}
id[0].push_back(st);
dist[st] = 0;
for(int i = 0 ; i < MX; i ++ ){
if(id[i].empty()) continue;
for(auto x : id[i]){ // this just adds +n so its fine
if(x == en){
cout << i << "\n";
return 0;
}
}
for(auto x : id[i]){
if(dist[x] != i) continue;
for(auto y : q[x]){
for(int f = 1; x + y * f < n && i + f < MX; f ++ ){
if(dist[x + y * f] > i + f){
dist[x + y * f] = i + f;
id[i + f].push_back(x + y * f);
}
}
for(int f = 1; x - y * f >= 0 && i + f < MX; f ++ ){
if(dist[x - y * f] > i + f){
dist[x - y * f] = i + f;
id[i + f].push_back(x - y * f);
}
}
}
}
}
cout << "-1\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24576 KB |
Output is correct |
2 |
Correct |
17 ms |
24576 KB |
Output is correct |
3 |
Correct |
18 ms |
24576 KB |
Output is correct |
4 |
Correct |
21 ms |
24576 KB |
Output is correct |
5 |
Correct |
18 ms |
24576 KB |
Output is correct |
6 |
Correct |
18 ms |
24576 KB |
Output is correct |
7 |
Correct |
20 ms |
24576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24576 KB |
Output is correct |
2 |
Correct |
18 ms |
24576 KB |
Output is correct |
3 |
Correct |
18 ms |
24576 KB |
Output is correct |
4 |
Correct |
26 ms |
24568 KB |
Output is correct |
5 |
Correct |
19 ms |
24576 KB |
Output is correct |
6 |
Correct |
18 ms |
24568 KB |
Output is correct |
7 |
Correct |
19 ms |
24576 KB |
Output is correct |
8 |
Correct |
18 ms |
24576 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
21 ms |
24576 KB |
Output is correct |
11 |
Correct |
20 ms |
24576 KB |
Output is correct |
12 |
Correct |
18 ms |
24576 KB |
Output is correct |
13 |
Correct |
21 ms |
24576 KB |
Output is correct |
14 |
Correct |
19 ms |
24580 KB |
Output is correct |
15 |
Correct |
18 ms |
24576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24552 KB |
Output is correct |
2 |
Correct |
18 ms |
24448 KB |
Output is correct |
3 |
Correct |
18 ms |
24576 KB |
Output is correct |
4 |
Correct |
22 ms |
24576 KB |
Output is correct |
5 |
Correct |
19 ms |
24576 KB |
Output is correct |
6 |
Correct |
18 ms |
24576 KB |
Output is correct |
7 |
Correct |
18 ms |
24576 KB |
Output is correct |
8 |
Correct |
19 ms |
24576 KB |
Output is correct |
9 |
Correct |
22 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24576 KB |
Output is correct |
11 |
Correct |
18 ms |
24576 KB |
Output is correct |
12 |
Correct |
19 ms |
24576 KB |
Output is correct |
13 |
Correct |
20 ms |
24576 KB |
Output is correct |
14 |
Correct |
18 ms |
24576 KB |
Output is correct |
15 |
Correct |
18 ms |
24576 KB |
Output is correct |
16 |
Correct |
21 ms |
24576 KB |
Output is correct |
17 |
Correct |
18 ms |
24576 KB |
Output is correct |
18 |
Correct |
27 ms |
24560 KB |
Output is correct |
19 |
Correct |
21 ms |
24576 KB |
Output is correct |
20 |
Correct |
22 ms |
24704 KB |
Output is correct |
21 |
Correct |
22 ms |
24560 KB |
Output is correct |
22 |
Correct |
22 ms |
24576 KB |
Output is correct |
23 |
Correct |
22 ms |
24576 KB |
Output is correct |
24 |
Correct |
21 ms |
24648 KB |
Output is correct |
25 |
Correct |
21 ms |
24576 KB |
Output is correct |
26 |
Correct |
19 ms |
24576 KB |
Output is correct |
27 |
Correct |
18 ms |
24576 KB |
Output is correct |
28 |
Correct |
23 ms |
24696 KB |
Output is correct |
29 |
Correct |
18 ms |
24704 KB |
Output is correct |
30 |
Correct |
18 ms |
24576 KB |
Output is correct |
31 |
Correct |
18 ms |
24576 KB |
Output is correct |
32 |
Correct |
22 ms |
24576 KB |
Output is correct |
33 |
Correct |
19 ms |
24704 KB |
Output is correct |
34 |
Correct |
19 ms |
24704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24576 KB |
Output is correct |
2 |
Correct |
20 ms |
24448 KB |
Output is correct |
3 |
Correct |
18 ms |
24576 KB |
Output is correct |
4 |
Correct |
22 ms |
24576 KB |
Output is correct |
5 |
Correct |
18 ms |
24576 KB |
Output is correct |
6 |
Correct |
18 ms |
24576 KB |
Output is correct |
7 |
Correct |
18 ms |
24576 KB |
Output is correct |
8 |
Correct |
18 ms |
24576 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24576 KB |
Output is correct |
11 |
Correct |
19 ms |
24576 KB |
Output is correct |
12 |
Correct |
18 ms |
24576 KB |
Output is correct |
13 |
Correct |
18 ms |
24536 KB |
Output is correct |
14 |
Correct |
18 ms |
24576 KB |
Output is correct |
15 |
Correct |
18 ms |
24576 KB |
Output is correct |
16 |
Correct |
22 ms |
24528 KB |
Output is correct |
17 |
Correct |
18 ms |
24576 KB |
Output is correct |
18 |
Correct |
23 ms |
24576 KB |
Output is correct |
19 |
Correct |
21 ms |
24576 KB |
Output is correct |
20 |
Correct |
22 ms |
24704 KB |
Output is correct |
21 |
Correct |
20 ms |
24576 KB |
Output is correct |
22 |
Correct |
21 ms |
24496 KB |
Output is correct |
23 |
Correct |
18 ms |
24576 KB |
Output is correct |
24 |
Correct |
18 ms |
24576 KB |
Output is correct |
25 |
Correct |
19 ms |
24576 KB |
Output is correct |
26 |
Correct |
18 ms |
24576 KB |
Output is correct |
27 |
Correct |
17 ms |
24576 KB |
Output is correct |
28 |
Correct |
17 ms |
24704 KB |
Output is correct |
29 |
Correct |
17 ms |
24704 KB |
Output is correct |
30 |
Correct |
17 ms |
24576 KB |
Output is correct |
31 |
Correct |
20 ms |
24576 KB |
Output is correct |
32 |
Correct |
18 ms |
24704 KB |
Output is correct |
33 |
Correct |
23 ms |
24704 KB |
Output is correct |
34 |
Correct |
21 ms |
24704 KB |
Output is correct |
35 |
Correct |
23 ms |
24704 KB |
Output is correct |
36 |
Correct |
21 ms |
24576 KB |
Output is correct |
37 |
Correct |
22 ms |
24704 KB |
Output is correct |
38 |
Correct |
24 ms |
24824 KB |
Output is correct |
39 |
Correct |
23 ms |
24704 KB |
Output is correct |
40 |
Correct |
24 ms |
24832 KB |
Output is correct |
41 |
Correct |
24 ms |
24832 KB |
Output is correct |
42 |
Correct |
22 ms |
24784 KB |
Output is correct |
43 |
Correct |
27 ms |
24832 KB |
Output is correct |
44 |
Correct |
69 ms |
24820 KB |
Output is correct |
45 |
Correct |
22 ms |
24832 KB |
Output is correct |
46 |
Correct |
22 ms |
24832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24576 KB |
Output is correct |
2 |
Correct |
18 ms |
24552 KB |
Output is correct |
3 |
Correct |
20 ms |
24576 KB |
Output is correct |
4 |
Correct |
21 ms |
24576 KB |
Output is correct |
5 |
Correct |
20 ms |
24576 KB |
Output is correct |
6 |
Correct |
16 ms |
24576 KB |
Output is correct |
7 |
Correct |
16 ms |
24576 KB |
Output is correct |
8 |
Correct |
17 ms |
24576 KB |
Output is correct |
9 |
Correct |
16 ms |
24576 KB |
Output is correct |
10 |
Correct |
16 ms |
24576 KB |
Output is correct |
11 |
Correct |
23 ms |
24576 KB |
Output is correct |
12 |
Correct |
18 ms |
24576 KB |
Output is correct |
13 |
Correct |
18 ms |
24512 KB |
Output is correct |
14 |
Correct |
18 ms |
24576 KB |
Output is correct |
15 |
Correct |
17 ms |
24576 KB |
Output is correct |
16 |
Correct |
20 ms |
24576 KB |
Output is correct |
17 |
Correct |
17 ms |
24576 KB |
Output is correct |
18 |
Correct |
26 ms |
24576 KB |
Output is correct |
19 |
Correct |
20 ms |
24600 KB |
Output is correct |
20 |
Correct |
22 ms |
24704 KB |
Output is correct |
21 |
Correct |
24 ms |
24576 KB |
Output is correct |
22 |
Correct |
20 ms |
24576 KB |
Output is correct |
23 |
Correct |
18 ms |
24576 KB |
Output is correct |
24 |
Correct |
17 ms |
24576 KB |
Output is correct |
25 |
Correct |
17 ms |
24576 KB |
Output is correct |
26 |
Correct |
17 ms |
24576 KB |
Output is correct |
27 |
Correct |
22 ms |
24576 KB |
Output is correct |
28 |
Correct |
18 ms |
24704 KB |
Output is correct |
29 |
Correct |
17 ms |
24704 KB |
Output is correct |
30 |
Correct |
17 ms |
24704 KB |
Output is correct |
31 |
Correct |
17 ms |
24576 KB |
Output is correct |
32 |
Correct |
17 ms |
24576 KB |
Output is correct |
33 |
Correct |
17 ms |
24704 KB |
Output is correct |
34 |
Correct |
19 ms |
24704 KB |
Output is correct |
35 |
Correct |
22 ms |
24704 KB |
Output is correct |
36 |
Correct |
17 ms |
24576 KB |
Output is correct |
37 |
Correct |
22 ms |
24704 KB |
Output is correct |
38 |
Correct |
23 ms |
24832 KB |
Output is correct |
39 |
Correct |
23 ms |
24704 KB |
Output is correct |
40 |
Correct |
26 ms |
24824 KB |
Output is correct |
41 |
Correct |
23 ms |
24832 KB |
Output is correct |
42 |
Correct |
24 ms |
24832 KB |
Output is correct |
43 |
Correct |
21 ms |
24832 KB |
Output is correct |
44 |
Correct |
67 ms |
24820 KB |
Output is correct |
45 |
Correct |
24 ms |
24832 KB |
Output is correct |
46 |
Correct |
23 ms |
24832 KB |
Output is correct |
47 |
Correct |
32 ms |
25208 KB |
Output is correct |
48 |
Correct |
27 ms |
25088 KB |
Output is correct |
49 |
Correct |
26 ms |
25088 KB |
Output is correct |
50 |
Correct |
24 ms |
24960 KB |
Output is correct |
51 |
Correct |
29 ms |
26236 KB |
Output is correct |
52 |
Correct |
29 ms |
25980 KB |
Output is correct |
53 |
Correct |
26 ms |
25728 KB |
Output is correct |
54 |
Correct |
23 ms |
25216 KB |
Output is correct |
55 |
Correct |
22 ms |
25216 KB |
Output is correct |
56 |
Correct |
840 ms |
26496 KB |
Output is correct |
57 |
Correct |
21 ms |
25344 KB |
Output is correct |
58 |
Correct |
958 ms |
25848 KB |
Output is correct |
59 |
Correct |
24 ms |
25600 KB |
Output is correct |
60 |
Correct |
24 ms |
25600 KB |
Output is correct |
61 |
Correct |
24 ms |
25600 KB |
Output is correct |
62 |
Correct |
32 ms |
26496 KB |
Output is correct |
63 |
Correct |
30 ms |
26496 KB |
Output is correct |
64 |
Correct |
31 ms |
26624 KB |
Output is correct |
65 |
Correct |
33 ms |
26880 KB |
Output is correct |
66 |
Correct |
38 ms |
27128 KB |
Output is correct |
67 |
Correct |
40 ms |
27128 KB |
Output is correct |