This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct point{
int x, y;
};
int K, m;
int dis(point a, point b){
point d {abs(a.x - b.x), abs(a.y - b.y)};
if(m == 1) return d.x + d.y;
bool x1 = d.x % (m + 1), y1 = d.y % (m + 1);
int v_x = d.x / (m + 1) * (m + 1), v_y = d.y / (m + 1) * (m + 1);
int ans = 1LL << 60;
for(auto p : vector<point>{{v_x, v_y}, {v_x, v_y + (m + 1)}, {v_x + (m + 1), v_y}, {v_x + (m + 1), v_y + (m + 1)}}){
int res = p.x / (m + 1) * 2 + p.y / (m + 1) * 2;
res += abs(p.x - d.x) != 0 && p.y == 0 ? 2 + abs(p.x - d.x) : abs(p.x - d.x);
res += abs(p.y - d.y) != 0 && p.x == 0 ? 2 + abs(p.y - d.y) : abs(p.y - d.y);
ans = min(ans, res);
}
return ans;
}
int dp[501][101];
vector<point> layers[501];
int32_t main(){
cin >> K >> m;
int s_x, s_y, e_x, e_y;
cin >> s_x >> s_y >> e_x >> e_y;
point s {s_x, s_y}, e {e_x, e_y};
layers[0].push_back(s);
for(int i = 1; i < K; i++){
int l; cin >> l;
for(int j = 0; j < l; j++){
int x, y; cin >> x >> y;
layers[i].push_back({x, y});
}
}
layers[K].push_back(e);
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for(int i = 0; i < K; i++){
for(int j = 0; j < layers[i].size(); j++){
for(int k = 0; k < layers[i + 1].size(); k++){
dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + dis(layers[i][j], layers[i + 1][k]));
}
}
}
cout << dp[K][0] << endl;
}
Compilation message (stderr)
obelisk.cpp: In function 'long long int dis(point, point)':
obelisk.cpp:13:7: warning: unused variable 'x1' [-Wunused-variable]
13 | bool x1 = d.x % (m + 1), y1 = d.y % (m + 1);
| ^~
obelisk.cpp:13:27: warning: unused variable 'y1' [-Wunused-variable]
13 | bool x1 = d.x % (m + 1), y1 = d.y % (m + 1);
| ^~
obelisk.cpp: In function 'int32_t main()':
obelisk.cpp:45:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0; j < layers[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~
obelisk.cpp:46:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int k = 0; k < layers[i + 1].size(); k++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |