#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define mod 1000000007
#define sp << " " <<
#define endl << '\n'
long long int M, K;
vector<vector<array<long long, 3>>> holes;
long long go(long long x1, long long y1, long long x2, long long y2){
if (M == 1){
return abs(x1 - x2) + abs(y1 - y2);
}
bool k1 = abs(x1 - x2) < M + 1, k2 = abs(y1 - y2) < M + 1;
long long oriantex, oriantey;
oriantey = abs(y1 - y2) / (M + 1) * 2 + (abs(y1 - y2) % (M + 1));
oriantey = min(oriantey, abs(y1 - y2) / (M + 1) * 2 + 2 + M + 1 - (abs(y1 - y2) % (M + 1)) - (k2 && abs(x1 - x2) % (M + 1)) * 2);
if (k1 && abs(y1 - y2) % (M + 1)){
oriantey += 2;
}
oriantex = abs(x1 - x2) / (M + 1) * 2 + (abs(x1 - x2) % (M + 1));
oriantex = min(oriantex, abs(x1 - x2) / (M + 1) * 2 + 2 + M + 1 - (abs(x1 - x2) % (M + 1)) - (k1 && abs(y1 - y2) % (M + 1)) * 2);
if (k2 && abs(x1 - x2) % (M + 1)){
oriantex += 2;
}
return oriantex + oriantey;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
M = 10;
cerr << go(0, 0, 12, 10);
return 0;
cin >> K >> M;
long long bx, by, sx, sy;
cin >> bx >> by >> sx >> sy;
if (K == 1){
cout << go(bx, by, sx, sy);
return 0;
}
holes.resize(K - 1);
for (int i = 0; i < K - 1; i++){
int sk;
cin >> sk;
holes[i].resize(sk);
for (int j = 0; j < sk; j++){
cin >> holes[i][j][0] >> holes[i][j][1];
if (i == 0)
holes[i][j][2] = go(bx, by, holes[i][j][0], holes[i][j][1]);
}
}
for (int i = 1; i < K - 1; i++){
for (int j = holes[i].size() - 1; j >= 0; j--){
holes[i][j][2] = LONG_MAX;
for (int o = holes[i - 1].size() - 1; o >= 0; o--){
holes[i][j][2] = min(holes[i][j][2], go(holes[i - 1][o][0], holes[i - 1][o][1], holes[i][j][0], holes[i][j][1]) + holes[i - 1][o][2]);
}
}
}
long long ret = LONG_MAX;
for (int i = holes[K - 2].size() - 1; i >= 0; i--){
ret = min(ret, holes[K - 2][i][2] + go(sx, sy, holes[K - 2][i][0], holes[K - 2][i][1]));
}
cout << ret;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |