#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 3e4+5;
const int inf = 2e9;
int d[maxn][200], vis[maxn][200];
vector<pair<int, int>> vec[maxn];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
int n, m;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//ifstream cin("input.txt");
cin >> n >> m;
for(int i = 0; i < n; i++){
for(int j = 0; j < 200; j++)d[i][j]=2e9;
}
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
vec[a].push_back({b, i});
if(!i){
d[a][0]=0;
pq.push({0, {a, 0}});
}
}
int ans = -1;
while(!pq.empty()){
auto x = pq.top().s;
//cout << x.f << " " << x.s << endl;
pq.pop();
if(vis[x.f][x.s])continue;
if(x.s){
if(x.f - x.s >= 0 && d[x.f-x.s][x.s] > d[x.f][x.s] +1){
d[x.f-x.s][x.s] = d[x.f][x.s] +1;
pq.push({d[x.f-x.s][x.s], {x.f-x.s, x.s}});
}
if(x.f + x.s < n && d[x.f+x.s][x.s] > d[x.f][x.s] +1){
d[x.f+x.s][x.s] = d[x.f][x.s] +1;
pq.push({d[x.f+x.s][x.s], {x.f+x.s, x.s}});
}
if(d[x.f][0] > d[x.f][x.s]){
d[x.f][0]=d[x.f][x.s];
pq.push({d[x.f][0], {x.f, 0}});
}
}
else{
for(auto v: vec[x.f]){
if(v.s == 1)ans = d[x.f][0];
if(v.f < 200){
if(d[x.f][v.f] > d[x.f][0]){
d[x.f][v.f]=d[x.f][0];
pq.push({d[x.f][v.f], {x.f, v.f}});
}
}
else{
for(int j = 1; x.f-j*v.f >= 0; j++){
int pos = x.f-j*v.f;
if(d[pos][0] > d[x.f][x.s]+j){
d[pos][0] = d[x.f][x.s]+j;
pq.push({d[pos][0], {pos, 0}});
}
}
for(int j = 1; x.f+j*v.f < n; j++){
int pos = x.f+j*v.f;
if(d[pos][0] > d[x.f][x.s]+j){
d[pos][0] = d[x.f][x.s]+j;
pq.push({d[pos][0], {pos, 0}});
}
}
}
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1620 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
1296 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
3 ms |
2516 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
2644 KB |
Output is correct |
29 |
Correct |
8 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2616 KB |
Output is correct |
31 |
Correct |
7 ms |
2644 KB |
Output is correct |
32 |
Correct |
4 ms |
2516 KB |
Output is correct |
33 |
Correct |
14 ms |
2696 KB |
Output is correct |
34 |
Correct |
20 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
2 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
2 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1108 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
3 ms |
2516 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
3 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
8 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2516 KB |
Output is correct |
31 |
Correct |
5 ms |
2644 KB |
Output is correct |
32 |
Correct |
5 ms |
2644 KB |
Output is correct |
33 |
Correct |
16 ms |
2644 KB |
Output is correct |
34 |
Correct |
15 ms |
2644 KB |
Output is correct |
35 |
Correct |
15 ms |
2636 KB |
Output is correct |
36 |
Correct |
3 ms |
2012 KB |
Output is correct |
37 |
Correct |
20 ms |
3028 KB |
Output is correct |
38 |
Correct |
19 ms |
3228 KB |
Output is correct |
39 |
Correct |
19 ms |
3232 KB |
Output is correct |
40 |
Correct |
19 ms |
3232 KB |
Output is correct |
41 |
Correct |
20 ms |
3188 KB |
Output is correct |
42 |
Correct |
6 ms |
2900 KB |
Output is correct |
43 |
Correct |
6 ms |
2996 KB |
Output is correct |
44 |
Correct |
6 ms |
2940 KB |
Output is correct |
45 |
Correct |
62 ms |
3480 KB |
Output is correct |
46 |
Correct |
61 ms |
3480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
2 ms |
1208 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1620 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
2 ms |
2260 KB |
Output is correct |
24 |
Correct |
3 ms |
2516 KB |
Output is correct |
25 |
Correct |
3 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
8 ms |
2644 KB |
Output is correct |
30 |
Correct |
4 ms |
2516 KB |
Output is correct |
31 |
Correct |
5 ms |
2656 KB |
Output is correct |
32 |
Correct |
4 ms |
2516 KB |
Output is correct |
33 |
Correct |
15 ms |
2688 KB |
Output is correct |
34 |
Correct |
15 ms |
2700 KB |
Output is correct |
35 |
Correct |
13 ms |
2664 KB |
Output is correct |
36 |
Correct |
4 ms |
2004 KB |
Output is correct |
37 |
Correct |
20 ms |
3056 KB |
Output is correct |
38 |
Correct |
19 ms |
3156 KB |
Output is correct |
39 |
Correct |
19 ms |
3156 KB |
Output is correct |
40 |
Correct |
18 ms |
3156 KB |
Output is correct |
41 |
Correct |
20 ms |
3188 KB |
Output is correct |
42 |
Correct |
7 ms |
2900 KB |
Output is correct |
43 |
Correct |
6 ms |
2900 KB |
Output is correct |
44 |
Correct |
5 ms |
3020 KB |
Output is correct |
45 |
Correct |
60 ms |
3456 KB |
Output is correct |
46 |
Correct |
58 ms |
3480 KB |
Output is correct |
47 |
Correct |
85 ms |
11644 KB |
Output is correct |
48 |
Correct |
15 ms |
19924 KB |
Output is correct |
49 |
Correct |
18 ms |
21572 KB |
Output is correct |
50 |
Correct |
16 ms |
23380 KB |
Output is correct |
51 |
Correct |
58 ms |
26044 KB |
Output is correct |
52 |
Correct |
63 ms |
26004 KB |
Output is correct |
53 |
Correct |
26 ms |
25516 KB |
Output is correct |
54 |
Correct |
18 ms |
24596 KB |
Output is correct |
55 |
Correct |
16 ms |
24532 KB |
Output is correct |
56 |
Correct |
24 ms |
25648 KB |
Output is correct |
57 |
Correct |
83 ms |
24620 KB |
Output is correct |
58 |
Correct |
22 ms |
25060 KB |
Output is correct |
59 |
Correct |
24 ms |
25088 KB |
Output is correct |
60 |
Correct |
30 ms |
25120 KB |
Output is correct |
61 |
Correct |
24 ms |
25196 KB |
Output is correct |
62 |
Correct |
45 ms |
25816 KB |
Output is correct |
63 |
Correct |
55 ms |
26932 KB |
Output is correct |
64 |
Correct |
72 ms |
26724 KB |
Output is correct |
65 |
Correct |
328 ms |
26948 KB |
Output is correct |
66 |
Correct |
866 ms |
26748 KB |
Output is correct |
67 |
Correct |
888 ms |
26820 KB |
Output is correct |