#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize (03)
#define int long long
#define pb push_back
#define tii tuple<int,int,int>
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int maxn = 3e4 + 5, INF = 0x3f3f3f3f;
vector<int> doges[maxn];
int dist[maxn][300];
priority_queue<tii, vector<tii>, greater<tii>> pq;
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
pii doge0, doge1; cin >> doge0.ff >> doge0.ss >> doge1.ff >> doge1.ss;
doges[doge0.ff].pb(doge0.ss);
doges[doge1.ff].pb(doge1.ss);
for(int i = 2; i < m; i++){
int bi, pi; cin >> bi >> pi;
doges[bi].pb(pi);
}
for(int i = 0; i < n; i++){
sort(all(doges[i]));
doges[i].erase(unique(all(doges[i])), doges[i].end());
// cout << doges[i].size() << endl;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < 300; j++){
dist[i][j] = INF;
}
}
pq.push({0,doge0.ff, 0});
dist[doge0.ff][0] = 0;
while(pq.size()){
auto[d,u,extra] = pq.top(); pq.pop();
if(dist[u][extra]<d) continue;
// cout << u << ": " << dist[u] << endl;
if(u==doge1.ff) continue;
if(extra==0) for(int x : doges[u]){
if(x >= (int)sqrt(n)){
for(int i = u+x; i < n; i+=x){
int wv = (i-u)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
for(int i = u-x; i >= 0; i-=x){
int wv = (u-i)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
}
else{
if(u+x < n && dist[u+x][0] > d+1){
dist[u+x][0] = d+1, pq.push({dist[u+x][0], u+x, 0});
}
if(u+x < n && dist[u+x][x] > d+1){
dist[u+x][x] = d+1, pq.push({dist[u+x][x], u+x, x});
}
if(u-x >= 0 && dist[u-x][0] > d+1){
dist[u-x][0] = d+1, pq.push({dist[u-x][0], u-x, 0});
}
if(u-x >= 0 && dist[u-x][x] > d+1){
dist[u-x][x] = d+1, pq.push({dist[u-x][x], u-x, x});
}
}
}
if(extra){
if(u+extra < n && dist[u+extra][extra] > d+1){
dist[u+extra][extra] = d+1, pq.push({d+1, u+extra, extra});
}
if(u+extra < n && dist[u+extra][0] > d+1){
dist[u+extra][0] = d+1, pq.push({d+1, u+extra, 0});
}
if(u-extra >= 0 && dist[u-extra][extra] > d+1){
dist[u-extra][extra] = d+1, pq.push({d+1, u-extra, extra});
}
if(u-extra >= 0 && dist[u-extra][0] > d+1){
dist[u-extra][0] = d+1, pq.push({d+1, u-extra, 0});
}
}
}
if(dist[doge1.ff][0] == INF) cout << -1;
else cout << dist[doge1.ff][0];
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
1 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
5204 KB |
Output is correct |
23 |
Correct |
3 ms |
4820 KB |
Output is correct |
24 |
Correct |
3 ms |
5588 KB |
Output is correct |
25 |
Correct |
3 ms |
5716 KB |
Output is correct |
26 |
Correct |
3 ms |
5716 KB |
Output is correct |
27 |
Correct |
3 ms |
5716 KB |
Output is correct |
28 |
Correct |
3 ms |
5716 KB |
Output is correct |
29 |
Correct |
8 ms |
5716 KB |
Output is correct |
30 |
Correct |
4 ms |
5716 KB |
Output is correct |
31 |
Correct |
6 ms |
5840 KB |
Output is correct |
32 |
Correct |
5 ms |
5844 KB |
Output is correct |
33 |
Correct |
15 ms |
6040 KB |
Output is correct |
34 |
Correct |
13 ms |
6036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 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 |
1108 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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
2 ms |
1324 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5712 KB |
Output is correct |
20 |
Correct |
4 ms |
5816 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
3 ms |
5204 KB |
Output is correct |
23 |
Correct |
4 ms |
4820 KB |
Output is correct |
24 |
Correct |
3 ms |
5588 KB |
Output is correct |
25 |
Correct |
3 ms |
5716 KB |
Output is correct |
26 |
Correct |
3 ms |
5716 KB |
Output is correct |
27 |
Correct |
3 ms |
5716 KB |
Output is correct |
28 |
Correct |
3 ms |
5716 KB |
Output is correct |
29 |
Correct |
10 ms |
5716 KB |
Output is correct |
30 |
Correct |
4 ms |
5716 KB |
Output is correct |
31 |
Correct |
5 ms |
5844 KB |
Output is correct |
32 |
Correct |
4 ms |
5844 KB |
Output is correct |
33 |
Correct |
11 ms |
6040 KB |
Output is correct |
34 |
Correct |
13 ms |
6036 KB |
Output is correct |
35 |
Correct |
9 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
3668 KB |
Output is correct |
37 |
Correct |
12 ms |
6228 KB |
Output is correct |
38 |
Correct |
13 ms |
6612 KB |
Output is correct |
39 |
Correct |
14 ms |
6544 KB |
Output is correct |
40 |
Correct |
14 ms |
6612 KB |
Output is correct |
41 |
Correct |
12 ms |
6612 KB |
Output is correct |
42 |
Correct |
5 ms |
6228 KB |
Output is correct |
43 |
Correct |
5 ms |
6228 KB |
Output is correct |
44 |
Correct |
6 ms |
6268 KB |
Output is correct |
45 |
Correct |
16 ms |
6808 KB |
Output is correct |
46 |
Correct |
16 ms |
6808 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 |
1104 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 |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1300 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
1 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
5204 KB |
Output is correct |
23 |
Correct |
3 ms |
4820 KB |
Output is correct |
24 |
Correct |
3 ms |
5588 KB |
Output is correct |
25 |
Correct |
3 ms |
5716 KB |
Output is correct |
26 |
Correct |
3 ms |
5716 KB |
Output is correct |
27 |
Correct |
3 ms |
5716 KB |
Output is correct |
28 |
Correct |
3 ms |
5716 KB |
Output is correct |
29 |
Correct |
8 ms |
5716 KB |
Output is correct |
30 |
Correct |
4 ms |
5716 KB |
Output is correct |
31 |
Correct |
6 ms |
5716 KB |
Output is correct |
32 |
Correct |
4 ms |
5844 KB |
Output is correct |
33 |
Correct |
11 ms |
6040 KB |
Output is correct |
34 |
Correct |
11 ms |
6036 KB |
Output is correct |
35 |
Correct |
9 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
3652 KB |
Output is correct |
37 |
Correct |
12 ms |
6132 KB |
Output is correct |
38 |
Correct |
12 ms |
6612 KB |
Output is correct |
39 |
Correct |
12 ms |
6612 KB |
Output is correct |
40 |
Correct |
17 ms |
6620 KB |
Output is correct |
41 |
Correct |
13 ms |
6612 KB |
Output is correct |
42 |
Correct |
5 ms |
6228 KB |
Output is correct |
43 |
Correct |
5 ms |
6228 KB |
Output is correct |
44 |
Correct |
6 ms |
6268 KB |
Output is correct |
45 |
Correct |
16 ms |
6808 KB |
Output is correct |
46 |
Correct |
16 ms |
6808 KB |
Output is correct |
47 |
Correct |
70 ms |
31572 KB |
Output is correct |
48 |
Correct |
22 ms |
56192 KB |
Output is correct |
49 |
Correct |
23 ms |
61260 KB |
Output is correct |
50 |
Correct |
24 ms |
67056 KB |
Output is correct |
51 |
Correct |
75 ms |
73288 KB |
Output is correct |
52 |
Correct |
64 ms |
73288 KB |
Output is correct |
53 |
Correct |
35 ms |
72444 KB |
Output is correct |
54 |
Correct |
27 ms |
71380 KB |
Output is correct |
55 |
Correct |
25 ms |
71380 KB |
Output is correct |
56 |
Correct |
31 ms |
72516 KB |
Output is correct |
57 |
Correct |
84 ms |
71508 KB |
Output is correct |
58 |
Correct |
35 ms |
71888 KB |
Output is correct |
59 |
Correct |
31 ms |
72016 KB |
Output is correct |
60 |
Correct |
33 ms |
72020 KB |
Output is correct |
61 |
Correct |
34 ms |
72012 KB |
Output is correct |
62 |
Correct |
56 ms |
72652 KB |
Output is correct |
63 |
Correct |
66 ms |
75308 KB |
Output is correct |
64 |
Correct |
92 ms |
75288 KB |
Output is correct |
65 |
Correct |
263 ms |
75456 KB |
Output is correct |
66 |
Correct |
740 ms |
78272 KB |
Output is correct |
67 |
Correct |
727 ms |
78396 KB |
Output is correct |