#include <bits/stdc++.h>
using namespace std;
constexpr array<pair<int, int>, 4> mov = {{{-1, 0}, {0, -1}, {0, 1}, {1, 0}}};
constexpr int64_t INF = 0x3f3f3f3f3f3f3f3f;
struct elem {
int64_t d;
int u;
elem(int64_t d, int u) : d(d), u(u) {}
bool operator<(const elem& rhs) const {
return d > rhs.d;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef home
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int h, w, n;
int64_t a, b, c;
cin >> h >> w >> a >> b >> c >> n;
++h, ++w;
vector<pair<int, int>> player(n);
for (auto& [x, y] : player)
cin >> x >> y;
auto dist = [&]() {
vector<vector<int>> dist(h, vector<int>(w, -1));
queue<int> q;
for (const auto& [x, y] : player) {
dist[x][y] = 0;
q.emplace(x << 16 | y);
}
while (!q.empty()) {
auto x = q.front();
auto y = x & 0xffff;
x >>= 16;
q.pop();
for (const auto& [dx, dy] : mov) {
auto X = x + dx, Y = y + dy;
if (~X && X != h && ~Y && Y != w && dist[X][Y] == -1) {
dist[X][Y] = dist[x][y] + 1;
q.emplace(X << 16 | Y);
}
}
}
return dist;
}();
vector<vector<array<int64_t, 5>>> cost(h, vector<array<int64_t, 5>>(w, {INF, INF, INF, INF, INF}));
// 4bit, 12bit, 12bit
priority_queue<elem> pq;
pq.emplace(cost[player[0].first][player[0].second][4] = 0,
4 << 24 | player[0].first << 12 | player[0].second);
while (!pq.empty()) {
auto [d, dir] = pq.top();
auto y = dir & 0xfff;
dir >>= 12;
auto x = dir & 0xfff;
dir >>= 12;
pq.pop();
if (cost[x][y][dir] != d) continue;
if (dir == 4) {
for (int i = 0; i < 4; ++i)
if (cost[x][y][i] > cost[x][y][4] + b)
pq.emplace(cost[x][y][i] = cost[x][y][4] + b, i << 24 | x << 12 | y);
for (const auto& [dx, dy] : mov) {
auto X = x + dx, Y = y + dy;
if (~X && X != h && ~Y && Y != w && cost[X][Y][4] > cost[x][y][4] + c)
pq.emplace(cost[X][Y][4] = cost[x][y][4] + c, 4 << 24 | X << 12 | Y);
}
} else {
if (cost[x][y][4] > cost[x][y][dir] + c * dist[x][y])
pq.emplace(cost[x][y][4] = cost[x][y][dir] + c * dist[x][y], 4 << 24 | x << 12 | y);
auto X = x + mov[dir].first, Y = y + mov[dir].second;
if (~X && X != h && ~Y && Y != w && cost[X][Y][dir] > cost[x][y][dir] + a)
pq.emplace(cost[X][Y][dir] = cost[x][y][dir] + a, dir << 24 | X << 12 | Y);
}
}
cout << cost[player[n - 1].first][player[n - 1].second][4];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
4024 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
314 ms |
15400 KB |
Output is correct |
4 |
Correct |
301 ms |
15412 KB |
Output is correct |
5 |
Correct |
49 ms |
4388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
15392 KB |
Output is correct |
2 |
Correct |
315 ms |
15400 KB |
Output is correct |
3 |
Correct |
226 ms |
13196 KB |
Output is correct |
4 |
Correct |
221 ms |
15384 KB |
Output is correct |
5 |
Correct |
205 ms |
13204 KB |
Output is correct |
6 |
Correct |
244 ms |
15356 KB |
Output is correct |
7 |
Correct |
282 ms |
15412 KB |
Output is correct |
8 |
Correct |
266 ms |
15384 KB |
Output is correct |
9 |
Correct |
278 ms |
15464 KB |
Output is correct |
10 |
Correct |
50 ms |
3256 KB |
Output is correct |
11 |
Correct |
315 ms |
15408 KB |
Output is correct |
12 |
Correct |
322 ms |
15412 KB |
Output is correct |
13 |
Correct |
170 ms |
13236 KB |
Output is correct |
14 |
Correct |
281 ms |
15328 KB |
Output is correct |
15 |
Correct |
195 ms |
13208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
4024 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
314 ms |
15400 KB |
Output is correct |
4 |
Correct |
301 ms |
15412 KB |
Output is correct |
5 |
Correct |
49 ms |
4388 KB |
Output is correct |
6 |
Correct |
298 ms |
15392 KB |
Output is correct |
7 |
Correct |
315 ms |
15400 KB |
Output is correct |
8 |
Correct |
226 ms |
13196 KB |
Output is correct |
9 |
Correct |
221 ms |
15384 KB |
Output is correct |
10 |
Correct |
205 ms |
13204 KB |
Output is correct |
11 |
Correct |
244 ms |
15356 KB |
Output is correct |
12 |
Correct |
282 ms |
15412 KB |
Output is correct |
13 |
Correct |
266 ms |
15384 KB |
Output is correct |
14 |
Correct |
278 ms |
15464 KB |
Output is correct |
15 |
Correct |
50 ms |
3256 KB |
Output is correct |
16 |
Correct |
315 ms |
15408 KB |
Output is correct |
17 |
Correct |
322 ms |
15412 KB |
Output is correct |
18 |
Correct |
170 ms |
13236 KB |
Output is correct |
19 |
Correct |
281 ms |
15328 KB |
Output is correct |
20 |
Correct |
195 ms |
13208 KB |
Output is correct |
21 |
Correct |
58 ms |
4660 KB |
Output is correct |
22 |
Correct |
368 ms |
15392 KB |
Output is correct |
23 |
Correct |
322 ms |
12212 KB |
Output is correct |
24 |
Correct |
366 ms |
13324 KB |
Output is correct |
25 |
Correct |
341 ms |
15408 KB |
Output is correct |
26 |
Correct |
327 ms |
15520 KB |
Output is correct |
27 |
Correct |
214 ms |
12388 KB |
Output is correct |
28 |
Correct |
185 ms |
12840 KB |
Output is correct |
29 |
Correct |
299 ms |
14888 KB |
Output is correct |
30 |
Correct |
165 ms |
12776 KB |
Output is correct |
31 |
Correct |
291 ms |
15412 KB |
Output is correct |
32 |
Correct |
393 ms |
17056 KB |
Output is correct |
33 |
Correct |
250 ms |
15304 KB |
Output is correct |
34 |
Correct |
372 ms |
15376 KB |
Output is correct |
35 |
Correct |
147 ms |
12740 KB |
Output is correct |