#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int K = 5;
const int MAXN = 30000;
vector <pair <int, int>> graf[K*MAXN+5];
int dist[K*MAXN+5];
const int INF = 1000000000;
int gde[MAXN+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
for(int d=1; d<K; d++){
for(int i=0; i<n; i++) graf[K*i+d].push_back({K*i, 0});
for(int i=0; i+d<n; i++) graf[K*i+d].push_back({K*(i+d) + d, 1});
for(int i=n-1; i-d>=0; i--) graf[K*i+d].push_back({K*(i-d) + d, 1});
}
for(int j=0; j<m; j++){
int x, d;
cin >> x >> d;
gde[j] = K*x;
if(d < K) graf[K*x].push_back({K*x+d, 0});
else{
for(int i=x+d; i<n; i+=d) graf[K*x].push_back({K*i, (i-x)/d});
for(int i=x-d; i>=0; i-=d) graf[K*x].push_back({K*i, (x-i)/d});
}
}
for(int i=0; i<n*K; i++) dist[i] = INF;
dist[gde[0]] = 0;
set <pair <int, int>> q;
q.insert({0, gde[0]});
while(!q.empty()){
int v = q.begin()->second;
q.erase(q.begin());
for(auto pr : graf[v]){
int c = pr.first, d = pr.second;
if(dist[c] > dist[v] + d){
q.erase({dist[c], c});
dist[c] = dist[v] + d;
q.insert({dist[c], c});
}
}
}
if(dist[gde[1]] == INF) cout << "-1\n";
else cout << dist[gde[1]] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
Output is correct |
2 |
Correct |
3 ms |
3788 KB |
Output is correct |
3 |
Correct |
3 ms |
3788 KB |
Output is correct |
4 |
Correct |
4 ms |
3832 KB |
Output is correct |
5 |
Correct |
3 ms |
3788 KB |
Output is correct |
6 |
Correct |
2 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
Output is correct |
2 |
Correct |
3 ms |
3788 KB |
Output is correct |
3 |
Correct |
4 ms |
3788 KB |
Output is correct |
4 |
Correct |
3 ms |
3788 KB |
Output is correct |
5 |
Correct |
3 ms |
3788 KB |
Output is correct |
6 |
Correct |
2 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3788 KB |
Output is correct |
8 |
Correct |
2 ms |
3788 KB |
Output is correct |
9 |
Correct |
3 ms |
3840 KB |
Output is correct |
10 |
Correct |
4 ms |
3840 KB |
Output is correct |
11 |
Correct |
3 ms |
3916 KB |
Output is correct |
12 |
Correct |
3 ms |
3900 KB |
Output is correct |
13 |
Correct |
5 ms |
3916 KB |
Output is correct |
14 |
Correct |
3 ms |
3840 KB |
Output is correct |
15 |
Correct |
3 ms |
3916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
Output is correct |
2 |
Correct |
3 ms |
3788 KB |
Output is correct |
3 |
Correct |
2 ms |
3788 KB |
Output is correct |
4 |
Correct |
3 ms |
3788 KB |
Output is correct |
5 |
Correct |
3 ms |
3788 KB |
Output is correct |
6 |
Correct |
3 ms |
3840 KB |
Output is correct |
7 |
Correct |
3 ms |
3836 KB |
Output is correct |
8 |
Correct |
3 ms |
3788 KB |
Output is correct |
9 |
Correct |
3 ms |
3788 KB |
Output is correct |
10 |
Correct |
3 ms |
3788 KB |
Output is correct |
11 |
Correct |
4 ms |
3916 KB |
Output is correct |
12 |
Correct |
3 ms |
3916 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
3 ms |
3916 KB |
Output is correct |
15 |
Correct |
3 ms |
3916 KB |
Output is correct |
16 |
Correct |
3 ms |
3916 KB |
Output is correct |
17 |
Correct |
5 ms |
4172 KB |
Output is correct |
18 |
Correct |
4 ms |
4172 KB |
Output is correct |
19 |
Correct |
5 ms |
4300 KB |
Output is correct |
20 |
Correct |
5 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
3916 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
6 ms |
4172 KB |
Output is correct |
24 |
Correct |
5 ms |
4428 KB |
Output is correct |
25 |
Correct |
6 ms |
4300 KB |
Output is correct |
26 |
Correct |
5 ms |
4300 KB |
Output is correct |
27 |
Correct |
5 ms |
4300 KB |
Output is correct |
28 |
Correct |
5 ms |
4384 KB |
Output is correct |
29 |
Correct |
10 ms |
4940 KB |
Output is correct |
30 |
Correct |
5 ms |
4428 KB |
Output is correct |
31 |
Correct |
6 ms |
4604 KB |
Output is correct |
32 |
Correct |
6 ms |
4556 KB |
Output is correct |
33 |
Correct |
12 ms |
5488 KB |
Output is correct |
34 |
Correct |
10 ms |
5508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3840 KB |
Output is correct |
2 |
Correct |
2 ms |
3788 KB |
Output is correct |
3 |
Correct |
2 ms |
3788 KB |
Output is correct |
4 |
Correct |
3 ms |
3788 KB |
Output is correct |
5 |
Correct |
2 ms |
3788 KB |
Output is correct |
6 |
Correct |
3 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3788 KB |
Output is correct |
8 |
Correct |
3 ms |
3840 KB |
Output is correct |
9 |
Correct |
3 ms |
3844 KB |
Output is correct |
10 |
Correct |
3 ms |
3792 KB |
Output is correct |
11 |
Correct |
3 ms |
3852 KB |
Output is correct |
12 |
Correct |
3 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
3 ms |
3848 KB |
Output is correct |
15 |
Correct |
4 ms |
3916 KB |
Output is correct |
16 |
Correct |
3 ms |
3916 KB |
Output is correct |
17 |
Correct |
5 ms |
4172 KB |
Output is correct |
18 |
Correct |
4 ms |
4172 KB |
Output is correct |
19 |
Correct |
4 ms |
4300 KB |
Output is correct |
20 |
Correct |
5 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
3848 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
5 ms |
4172 KB |
Output is correct |
24 |
Correct |
6 ms |
4428 KB |
Output is correct |
25 |
Correct |
5 ms |
4232 KB |
Output is correct |
26 |
Correct |
5 ms |
4300 KB |
Output is correct |
27 |
Correct |
5 ms |
4280 KB |
Output is correct |
28 |
Correct |
5 ms |
4428 KB |
Output is correct |
29 |
Correct |
8 ms |
4940 KB |
Output is correct |
30 |
Correct |
5 ms |
4428 KB |
Output is correct |
31 |
Correct |
6 ms |
4560 KB |
Output is correct |
32 |
Correct |
5 ms |
4428 KB |
Output is correct |
33 |
Correct |
10 ms |
5588 KB |
Output is correct |
34 |
Correct |
10 ms |
5456 KB |
Output is correct |
35 |
Correct |
14 ms |
5428 KB |
Output is correct |
36 |
Correct |
6 ms |
4300 KB |
Output is correct |
37 |
Correct |
17 ms |
6924 KB |
Output is correct |
38 |
Correct |
23 ms |
6564 KB |
Output is correct |
39 |
Correct |
18 ms |
6548 KB |
Output is correct |
40 |
Correct |
18 ms |
6524 KB |
Output is correct |
41 |
Correct |
18 ms |
6496 KB |
Output is correct |
42 |
Correct |
11 ms |
4996 KB |
Output is correct |
43 |
Correct |
9 ms |
4944 KB |
Output is correct |
44 |
Correct |
10 ms |
4904 KB |
Output is correct |
45 |
Correct |
21 ms |
9292 KB |
Output is correct |
46 |
Correct |
21 ms |
9176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3788 KB |
Output is correct |
2 |
Correct |
3 ms |
3788 KB |
Output is correct |
3 |
Correct |
3 ms |
3788 KB |
Output is correct |
4 |
Correct |
3 ms |
3788 KB |
Output is correct |
5 |
Correct |
3 ms |
3788 KB |
Output is correct |
6 |
Correct |
3 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3788 KB |
Output is correct |
8 |
Correct |
3 ms |
3788 KB |
Output is correct |
9 |
Correct |
3 ms |
3788 KB |
Output is correct |
10 |
Correct |
3 ms |
3788 KB |
Output is correct |
11 |
Correct |
4 ms |
3916 KB |
Output is correct |
12 |
Correct |
3 ms |
3916 KB |
Output is correct |
13 |
Correct |
3 ms |
3916 KB |
Output is correct |
14 |
Correct |
4 ms |
3916 KB |
Output is correct |
15 |
Correct |
3 ms |
3916 KB |
Output is correct |
16 |
Correct |
3 ms |
3916 KB |
Output is correct |
17 |
Correct |
5 ms |
4096 KB |
Output is correct |
18 |
Correct |
4 ms |
4172 KB |
Output is correct |
19 |
Correct |
4 ms |
4300 KB |
Output is correct |
20 |
Correct |
4 ms |
4300 KB |
Output is correct |
21 |
Correct |
3 ms |
3916 KB |
Output is correct |
22 |
Correct |
4 ms |
4172 KB |
Output is correct |
23 |
Correct |
5 ms |
4172 KB |
Output is correct |
24 |
Correct |
6 ms |
4360 KB |
Output is correct |
25 |
Correct |
6 ms |
4252 KB |
Output is correct |
26 |
Correct |
5 ms |
4300 KB |
Output is correct |
27 |
Correct |
4 ms |
4236 KB |
Output is correct |
28 |
Correct |
6 ms |
4428 KB |
Output is correct |
29 |
Correct |
8 ms |
4940 KB |
Output is correct |
30 |
Correct |
5 ms |
4428 KB |
Output is correct |
31 |
Correct |
6 ms |
4652 KB |
Output is correct |
32 |
Correct |
5 ms |
4428 KB |
Output is correct |
33 |
Correct |
10 ms |
5512 KB |
Output is correct |
34 |
Correct |
11 ms |
5564 KB |
Output is correct |
35 |
Correct |
13 ms |
5452 KB |
Output is correct |
36 |
Correct |
6 ms |
4240 KB |
Output is correct |
37 |
Correct |
16 ms |
6860 KB |
Output is correct |
38 |
Correct |
18 ms |
6476 KB |
Output is correct |
39 |
Correct |
18 ms |
6476 KB |
Output is correct |
40 |
Correct |
18 ms |
6476 KB |
Output is correct |
41 |
Correct |
17 ms |
6436 KB |
Output is correct |
42 |
Correct |
10 ms |
5000 KB |
Output is correct |
43 |
Correct |
11 ms |
4880 KB |
Output is correct |
44 |
Correct |
10 ms |
4964 KB |
Output is correct |
45 |
Correct |
21 ms |
9356 KB |
Output is correct |
46 |
Correct |
21 ms |
9164 KB |
Output is correct |
47 |
Correct |
72 ms |
15996 KB |
Output is correct |
48 |
Correct |
29 ms |
11092 KB |
Output is correct |
49 |
Correct |
29 ms |
10732 KB |
Output is correct |
50 |
Correct |
35 ms |
11212 KB |
Output is correct |
51 |
Correct |
75 ms |
13980 KB |
Output is correct |
52 |
Correct |
84 ms |
14208 KB |
Output is correct |
53 |
Correct |
34 ms |
10588 KB |
Output is correct |
54 |
Correct |
23 ms |
9944 KB |
Output is correct |
55 |
Correct |
28 ms |
10060 KB |
Output is correct |
56 |
Correct |
36 ms |
11216 KB |
Output is correct |
57 |
Correct |
225 ms |
52400 KB |
Output is correct |
58 |
Correct |
32 ms |
10820 KB |
Output is correct |
59 |
Correct |
37 ms |
11456 KB |
Output is correct |
60 |
Correct |
40 ms |
12000 KB |
Output is correct |
61 |
Correct |
39 ms |
11208 KB |
Output is correct |
62 |
Correct |
54 ms |
14804 KB |
Output is correct |
63 |
Correct |
136 ms |
33656 KB |
Output is correct |
64 |
Correct |
139 ms |
41512 KB |
Output is correct |
65 |
Correct |
177 ms |
47800 KB |
Output is correct |
66 |
Correct |
276 ms |
77380 KB |
Output is correct |
67 |
Correct |
267 ms |
80184 KB |
Output is correct |