#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int K = 2;
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;
using T = pair <int, int>;
priority_queue <T, vector <T>, greater <T>> q;
q.push({0, gde[0]});
while(!q.empty()){
int v, dr;
tie(dr, v) = q.top();
q.pop();
if(dr != dist[v]) continue;
for(auto pr : graf[v]){
int c = pr.first, d = pr.second;
if(dist[c] > dist[v] + d){
dist[c] = dist[v] + d;
q.push({dist[c], c});
}
}
}
if(dist[gde[1]] == INF) cout << "-1\n";
else cout << dist[gde[1]] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
1 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
1 ms |
1740 KB |
Output is correct |
14 |
Correct |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1792 KB |
Output is correct |
17 |
Correct |
3 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
1868 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1868 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
1996 KB |
Output is correct |
25 |
Correct |
3 ms |
1868 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1868 KB |
Output is correct |
28 |
Correct |
3 ms |
1996 KB |
Output is correct |
29 |
Correct |
4 ms |
2508 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
3 ms |
2252 KB |
Output is correct |
32 |
Correct |
3 ms |
1996 KB |
Output is correct |
33 |
Correct |
6 ms |
3252 KB |
Output is correct |
34 |
Correct |
7 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1636 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1744 KB |
Output is correct |
14 |
Correct |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1868 KB |
Output is correct |
20 |
Correct |
3 ms |
1868 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1868 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
1996 KB |
Output is correct |
25 |
Correct |
3 ms |
1868 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
2 ms |
1868 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
4 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
3 ms |
2252 KB |
Output is correct |
32 |
Correct |
2 ms |
1996 KB |
Output is correct |
33 |
Correct |
5 ms |
3276 KB |
Output is correct |
34 |
Correct |
6 ms |
3276 KB |
Output is correct |
35 |
Correct |
10 ms |
3236 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
11 ms |
4812 KB |
Output is correct |
38 |
Correct |
13 ms |
4240 KB |
Output is correct |
39 |
Correct |
14 ms |
4300 KB |
Output is correct |
40 |
Correct |
14 ms |
4428 KB |
Output is correct |
41 |
Correct |
13 ms |
4300 KB |
Output is correct |
42 |
Correct |
7 ms |
2380 KB |
Output is correct |
43 |
Correct |
7 ms |
2252 KB |
Output is correct |
44 |
Correct |
7 ms |
2252 KB |
Output is correct |
45 |
Correct |
16 ms |
6796 KB |
Output is correct |
46 |
Correct |
15 ms |
6692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
2 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
1 ms |
1744 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1740 KB |
Output is correct |
14 |
Correct |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
2 ms |
1740 KB |
Output is correct |
16 |
Correct |
1 ms |
1740 KB |
Output is correct |
17 |
Correct |
2 ms |
1996 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
3 ms |
1868 KB |
Output is correct |
20 |
Correct |
2 ms |
1868 KB |
Output is correct |
21 |
Correct |
1 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1868 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
1996 KB |
Output is correct |
25 |
Correct |
3 ms |
1868 KB |
Output is correct |
26 |
Correct |
2 ms |
1868 KB |
Output is correct |
27 |
Correct |
3 ms |
1868 KB |
Output is correct |
28 |
Correct |
3 ms |
1996 KB |
Output is correct |
29 |
Correct |
4 ms |
2508 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
3 ms |
2252 KB |
Output is correct |
32 |
Correct |
2 ms |
1996 KB |
Output is correct |
33 |
Correct |
5 ms |
3276 KB |
Output is correct |
34 |
Correct |
6 ms |
3276 KB |
Output is correct |
35 |
Correct |
11 ms |
3236 KB |
Output is correct |
36 |
Correct |
3 ms |
1996 KB |
Output is correct |
37 |
Correct |
12 ms |
4768 KB |
Output is correct |
38 |
Correct |
13 ms |
4220 KB |
Output is correct |
39 |
Correct |
13 ms |
4388 KB |
Output is correct |
40 |
Correct |
13 ms |
4324 KB |
Output is correct |
41 |
Correct |
13 ms |
4300 KB |
Output is correct |
42 |
Correct |
7 ms |
2380 KB |
Output is correct |
43 |
Correct |
7 ms |
2252 KB |
Output is correct |
44 |
Correct |
7 ms |
2252 KB |
Output is correct |
45 |
Correct |
16 ms |
6732 KB |
Output is correct |
46 |
Correct |
16 ms |
6748 KB |
Output is correct |
47 |
Correct |
39 ms |
12704 KB |
Output is correct |
48 |
Correct |
16 ms |
5252 KB |
Output is correct |
49 |
Correct |
16 ms |
4684 KB |
Output is correct |
50 |
Correct |
12 ms |
4772 KB |
Output is correct |
51 |
Correct |
36 ms |
7120 KB |
Output is correct |
52 |
Correct |
42 ms |
7348 KB |
Output is correct |
53 |
Correct |
19 ms |
4172 KB |
Output is correct |
54 |
Correct |
8 ms |
3788 KB |
Output is correct |
55 |
Correct |
11 ms |
3788 KB |
Output is correct |
56 |
Correct |
21 ms |
4408 KB |
Output is correct |
57 |
Correct |
146 ms |
62676 KB |
Output is correct |
58 |
Correct |
14 ms |
4284 KB |
Output is correct |
59 |
Correct |
17 ms |
4872 KB |
Output is correct |
60 |
Correct |
17 ms |
5524 KB |
Output is correct |
61 |
Correct |
16 ms |
4680 KB |
Output is correct |
62 |
Correct |
26 ms |
8136 KB |
Output is correct |
63 |
Correct |
77 ms |
28016 KB |
Output is correct |
64 |
Correct |
87 ms |
35616 KB |
Output is correct |
65 |
Correct |
106 ms |
43096 KB |
Output is correct |
66 |
Correct |
172 ms |
75208 KB |
Output is correct |
67 |
Correct |
170 ms |
76668 KB |
Output is correct |