#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
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 |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
1252 KB |
Output is correct |
2 |
Correct |
88 ms |
1300 KB |
Output is correct |
3 |
Correct |
86 ms |
1264 KB |
Output is correct |
4 |
Correct |
80 ms |
1356 KB |
Output is correct |
5 |
Correct |
83 ms |
1284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
1356 KB |
Output is correct |
2 |
Correct |
113 ms |
1248 KB |
Output is correct |
3 |
Correct |
106 ms |
1336 KB |
Output is correct |
4 |
Correct |
117 ms |
1288 KB |
Output is correct |
5 |
Correct |
109 ms |
1228 KB |
Output is correct |