#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const ll INF = 1e16 + 5;
const int INF2 = 1e9 + 5;
int dist[501][501];
ll cost[3][501][501];
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
struct Vertex {
int type, x, y;
ll cost;
bool operator<(const Vertex& other) const {
return cost > other.cost;
}
};
void fill() {
for(int i = 0; i < 501; i++) {
for(int j = 0; j < 501; j++) {
dist[i][j] = INF2;
for(int k = 0; k < 3; k++) {
cost[k][i][j] = INF;
}
}
}
}
void flood(const vector<array<int, 2>>& p) {
queue<array<int, 2>> q;
for(auto& i : p) {
dist[i[0]][i[1]] = 0;
q.push({i[0], i[1]});
}
while(!q.empty()) {
auto v = q.front();
q.pop();
for(int d = 0; d < 4; d++) {
int X = v[0] + dx[d];
int Y = v[1] + dy[d];
if (!(X >= 0 && X <= 500 && Y >= 0 && Y <= 500)) continue;
if (dist[v[0]][v[1]] + 1 < dist[X][Y]) {
dist[X][Y] = dist[v[0]][v[1]] + 1;
q.push({X, Y});
}
}
}
}
void mini(int& a, int b) {
a = min(a, b);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int H, W, A, B, C;
cin >> H >> W >> A >> B >> C;
auto gali = [&](int X, int Y) {
return (X >= 0 && X <= H && Y >= 0 && Y <= W);
};
int n;
cin >> n;
fill();
vector<array<int, 2>> p(n);
for(auto& i : p) {
cin >> i[0] >> i[1];
}
//for(int i = 0; i <= H; i++) {
//for(int j = 0; j <= W; j++) {
//for(int k = 0; k < n; k++) {
//dist[i][j] = min(dist[i][j], abs(p[k][0] - i) + abs(p[k][1] - j));
//}
//}
//}
flood(p);
priority_queue<Vertex> q;
q.push({2, p[0][0], p[0][1], 0});
cost[2][p[0][0]][p[0][1]] = 0;
while(!q.empty()) {
auto v = q.top();
q.pop();
//cout << v.type << " " << v.cost << " " << v.x << " " << v.y << "\n";
if (v.cost != cost[v.type][v.x][v.y]) continue;
// moving w/ ball
if (v.type == 2) {
if (v.cost + B < cost[0][v.x][v.y]) {
cost[0][v.x][v.y] = v.cost + B;
q.push({0, v.x, v.y, v.cost + B});
}
if (v.cost + B < cost[1][v.x][v.y]) {
cost[1][v.x][v.y] = v.cost + B;
q.push({1, v.x, v.y, v.cost + B});
}
for(int d = 0; d < 4; d++) {
int X = v.x + dx[d];
int Y = v.y + dy[d];
if (!gali(X, Y)) continue;
if (v.cost + C < cost[2][X][Y]) {
cost[2][X][Y] = v.cost + C;
q.push({2, X, Y, v.cost + C});
}
}
}
// kick in x
else if (v.type == 1) {
if (gali(v.x+1, v.y) && v.cost + A < cost[1][v.x+1][v.y]) {
cost[1][v.x+1][v.y] = v.cost + A;
q.push({1, v.x+1, v.y, v.cost + A});
}
if (gali(v.x-1, v.y) && v.cost + A < cost[1][v.x-1][v.y]) {
cost[1][v.x-1][v.y] = v.cost + A;
q.push({1, v.x-1, v.y, v.cost + A});
}
if (v.cost + C * dist[v.x][v.y] < cost[2][v.x][v.y]) {
cost[2][v.x][v.y] = v.cost + C * dist[v.x][v.y];
q.push({2, v.x, v.y, v.cost + C * dist[v.x][v.y]});
}
}
// kick in Y
else {
if (gali(v.x, v.y+1) && v.cost + A < cost[0][v.x][v.y+1]) {
cost[0][v.x][v.y+1] = v.cost + A;
q.push({0, v.x, v.y+1, v.cost + A});
}
if (gali(v.x, v.y-1) && v.cost + A < cost[0][v.x][v.y-1]) {
cost[0][v.x][v.y-1] = v.cost + A;
q.push({0, v.x, v.y-1, v.cost + A});
}
if (v.cost + C * dist[v.x][v.y] < cost[2][v.x][v.y]) {
cost[2][v.x][v.y] = v.cost + C * dist[v.x][v.y];
q.push({2, v.x, v.y, v.cost + C * dist[v.x][v.y]});
}
}
}
cout << cost[2][p.back()[0]][p.back()[1]] << "\n";
//for(int i = 0; i < n; i++) {
//cout << cost[2][p[i][0]][p[i][1]] << "\n";
//}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
10324 KB |
Output is correct |
2 |
Correct |
7 ms |
8148 KB |
Output is correct |
3 |
Correct |
162 ms |
16456 KB |
Output is correct |
4 |
Correct |
182 ms |
16496 KB |
Output is correct |
5 |
Correct |
23 ms |
8300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
16544 KB |
Output is correct |
2 |
Correct |
161 ms |
16496 KB |
Output is correct |
3 |
Correct |
118 ms |
16544 KB |
Output is correct |
4 |
Correct |
116 ms |
16472 KB |
Output is correct |
5 |
Correct |
138 ms |
16536 KB |
Output is correct |
6 |
Correct |
132 ms |
16460 KB |
Output is correct |
7 |
Correct |
161 ms |
16664 KB |
Output is correct |
8 |
Correct |
138 ms |
16676 KB |
Output is correct |
9 |
Correct |
196 ms |
16624 KB |
Output is correct |
10 |
Correct |
27 ms |
10332 KB |
Output is correct |
11 |
Correct |
132 ms |
16704 KB |
Output is correct |
12 |
Correct |
135 ms |
16468 KB |
Output is correct |
13 |
Correct |
99 ms |
16476 KB |
Output is correct |
14 |
Correct |
161 ms |
16688 KB |
Output is correct |
15 |
Correct |
112 ms |
16676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
10324 KB |
Output is correct |
2 |
Correct |
7 ms |
8148 KB |
Output is correct |
3 |
Correct |
162 ms |
16456 KB |
Output is correct |
4 |
Correct |
182 ms |
16496 KB |
Output is correct |
5 |
Correct |
23 ms |
8300 KB |
Output is correct |
6 |
Correct |
166 ms |
16544 KB |
Output is correct |
7 |
Correct |
161 ms |
16496 KB |
Output is correct |
8 |
Correct |
118 ms |
16544 KB |
Output is correct |
9 |
Correct |
116 ms |
16472 KB |
Output is correct |
10 |
Correct |
138 ms |
16536 KB |
Output is correct |
11 |
Correct |
132 ms |
16460 KB |
Output is correct |
12 |
Correct |
161 ms |
16664 KB |
Output is correct |
13 |
Correct |
138 ms |
16676 KB |
Output is correct |
14 |
Correct |
196 ms |
16624 KB |
Output is correct |
15 |
Correct |
27 ms |
10332 KB |
Output is correct |
16 |
Correct |
132 ms |
16704 KB |
Output is correct |
17 |
Correct |
135 ms |
16468 KB |
Output is correct |
18 |
Correct |
99 ms |
16476 KB |
Output is correct |
19 |
Correct |
161 ms |
16688 KB |
Output is correct |
20 |
Correct |
112 ms |
16676 KB |
Output is correct |
21 |
Correct |
32 ms |
8532 KB |
Output is correct |
22 |
Correct |
223 ms |
16492 KB |
Output is correct |
23 |
Correct |
173 ms |
12420 KB |
Output is correct |
24 |
Correct |
189 ms |
12656 KB |
Output is correct |
25 |
Correct |
208 ms |
16572 KB |
Output is correct |
26 |
Correct |
181 ms |
16820 KB |
Output is correct |
27 |
Correct |
103 ms |
11408 KB |
Output is correct |
28 |
Correct |
85 ms |
12092 KB |
Output is correct |
29 |
Correct |
171 ms |
14388 KB |
Output is correct |
30 |
Correct |
75 ms |
11476 KB |
Output is correct |
31 |
Correct |
179 ms |
16684 KB |
Output is correct |
32 |
Correct |
202 ms |
18400 KB |
Output is correct |
33 |
Correct |
155 ms |
16504 KB |
Output is correct |
34 |
Correct |
188 ms |
16744 KB |
Output is correct |
35 |
Correct |
68 ms |
11444 KB |
Output is correct |