#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
struct E {
int to, w;
};
const int K = 505, H = 105, N = K * H, INF = 1e9;
vector<pair<int, int>> holes[K];
vector<E> g2[N];
int s[K], d2[N], dx[3] = {-1, 1, 0};
int m, k, sx, sy, ex, ey;
int calc(int x, int y) {
if (m == 1)
return abs(x) + abs(y);
int ans = INF;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int g = 0; g < 2; g++) {
for (int h = 0; h < 2; h++) {
int p = abs(x + dx[i] * (m + 1)),
q = abs(y + dx[j] * (m + 1)),
t1 = p / (m + 1) + (i < 2),
t2 = q / (m + 1) + (j < 2),
cur = t1 * 2 + t2 * 2 + g * 2 + h * 2;
p %= (m + 1);
q %= (m + 1);
if (p) {
if (!t2)
cur += 2;
if (g)
cur += m + 1 - p;
else
cur += p;
}
if (q) {
if (!t1)
cur += 2;
if (h)
cur += m + 1 - q;
else
cur += q;
}
ans = min(ans, cur);
}
}
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> m >> sx >> sy >> ex >> ey;
holes[k].push_back({sx, sy});
holes[0].push_back({ex, ey});
for (int i = k - 1; i > 0; i--) {
int h;
cin >> h;
while (h--) {
int x, y;
cin >> x >> y;
holes[i].push_back({x, y});
}
}
for (int i = 0; i < k; i++) {
s[i + 1] = s[i] + holes[i].size();
}
for (int i = 1; i <= k; i++) {
for (int x = 0; x < holes[i].size(); x++) {
for (int y = 0; y < holes[i - 1].size(); y++) {
int dist = calc(holes[i - 1][y].first - holes[i][x].first,
holes[i - 1][y].second - holes[i][x].second);
g2[x + s[i]].push_back({y + s[i - 1], dist});
}
}
}
priority_queue<pair<int, int>,
vector<pair<int, int>>,
greater<pair<int, int>>> pq;
fill(d2, d2 + N, INF);
d2[s[k]] = 0;
pq.push({0, s[k]});
while (!pq.empty()) {
auto p = pq.top();
pq.pop();
if (p.first > d2[p.second])
continue;
int node = p.second;
for (E e : g2[node]) {
if (d2[e.to] > d2[node] + e.w) {
d2[e.to] = d2[node] + e.w;
pq.push({d2[e.to], e.to});
}
}
}
cout << d2[0] << "\n";
return 0;
}
Compilation message
obelisk.cpp: In function 'int main()':
obelisk.cpp:87:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int x = 0; x < holes[i].size(); x++) {
~~^~~~~~~~~~~~~~~~~
obelisk.cpp:88:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int y = 0; y < holes[i - 1].size(); y++) {
~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1784 KB |
Output is correct |
2 |
Correct |
2 ms |
1784 KB |
Output is correct |
3 |
Correct |
4 ms |
2040 KB |
Output is correct |
4 |
Correct |
3 ms |
1784 KB |
Output is correct |
5 |
Correct |
3 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1912 KB |
Output is correct |
2 |
Correct |
8 ms |
2044 KB |
Output is correct |
3 |
Correct |
9 ms |
2140 KB |
Output is correct |
4 |
Correct |
12 ms |
2296 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
15500 KB |
Output is correct |
2 |
Correct |
336 ms |
17256 KB |
Output is correct |
3 |
Correct |
328 ms |
16564 KB |
Output is correct |
4 |
Correct |
337 ms |
17104 KB |
Output is correct |
5 |
Correct |
342 ms |
17784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
16556 KB |
Output is correct |
2 |
Correct |
318 ms |
15908 KB |
Output is correct |
3 |
Correct |
317 ms |
16248 KB |
Output is correct |
4 |
Correct |
354 ms |
17816 KB |
Output is correct |
5 |
Correct |
329 ms |
16740 KB |
Output is correct |