#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
int h, w, a, b, c;
cin >> h >> w >> a >> b >> c;
int n; cin >> n;
vector<ar<int, 2>> p(n);
for (auto &x : p) cin >> x[0] >> x[1];
// dp[x][y] = min cost to have a player at (x, y) possessing the ball
// after a player gets the ball and moves and kicks the ball, they will not move again
const int dx[]{0, 1, -1, 0}, dy[]{1, 0, 0, -1};
auto in = [&](int x, int y) {
return x >= 0 && y >= 0 && x <= h && y <= w;
};
vector<vector<int>> dp2(h+1, vector<int>(w+1, 1e9));
{
queue<ar<int, 2>> q;
for (int i = 0; i < n; i++) {
auto [x, y] = p[i];
if (ckmin(dp2[x][y], 0)) q.push({x, y});
}
while (q.size()) {
auto [x, y] = q.front(); q.pop();
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (!in(nx, ny)) continue;
if (ckmin(dp2[nx][ny], dp2[x][y] + 1)) {
q.push({nx, ny});
}
}
}
}
const ll INF = 1e18;
vector<vector<ll>> dp(h+1, vector<ll>(w+1, INF));
dp[p[0][0]][p[0][1]] = 0;
set<ar<ll, 3>> q;
q.insert({0, p[0][0], p[0][1]});
while (q.size()) {
auto [weight, x, y] = *q.begin();
q.erase(q.begin());
// spread by walking
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
ll new_cost = dp[x][y] + c;
if (!in(nx, ny) || dp[nx][ny] <= new_cost) continue;
if (dp[nx][ny] != INF) {
q.erase({dp[nx][ny], nx, ny});
}
dp[nx][ny] = new_cost;
q.insert({new_cost, nx, ny});
}
// spread by kicking and having someone pick it up
for (int d = 0; d < 4; d++) {
for (int use = 1; use <= max(h, w); use++) {
int nx = x + dx[d] * use, ny = y + dy[d] * use;
if (!in(nx, ny)) break;
ll new_cost = dp[x][y] + (ll)dp2[nx][ny] * c + (ll)use * a + b;
if (dp[x][y] + (ll)use * a > dp[nx][ny]) break;
if (dp[nx][ny] <= new_cost) continue;
if (dp[nx][ny] != INF) {
q.erase({dp[nx][ny], nx, ny});
}
dp[nx][ny] = new_cost;
q.insert({new_cost, nx, ny});
}
}
}
cout << dp[p[n-1][0]][p[n-1][1]] << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
3416 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
430 ms |
12740 KB |
Output is correct |
4 |
Correct |
416 ms |
13668 KB |
Output is correct |
5 |
Correct |
116 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
16976 KB |
Output is correct |
2 |
Correct |
298 ms |
17540 KB |
Output is correct |
3 |
Correct |
269 ms |
12624 KB |
Output is correct |
4 |
Correct |
401 ms |
11848 KB |
Output is correct |
5 |
Correct |
225 ms |
12624 KB |
Output is correct |
6 |
Correct |
431 ms |
15640 KB |
Output is correct |
7 |
Correct |
237 ms |
18768 KB |
Output is correct |
8 |
Correct |
349 ms |
18780 KB |
Output is correct |
9 |
Correct |
266 ms |
18700 KB |
Output is correct |
10 |
Correct |
28 ms |
3160 KB |
Output is correct |
11 |
Correct |
254 ms |
18832 KB |
Output is correct |
12 |
Correct |
284 ms |
17996 KB |
Output is correct |
13 |
Correct |
307 ms |
13392 KB |
Output is correct |
14 |
Correct |
246 ms |
18768 KB |
Output is correct |
15 |
Correct |
178 ms |
14932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
3416 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
430 ms |
12740 KB |
Output is correct |
4 |
Correct |
416 ms |
13668 KB |
Output is correct |
5 |
Correct |
116 ms |
5496 KB |
Output is correct |
6 |
Correct |
365 ms |
16976 KB |
Output is correct |
7 |
Correct |
298 ms |
17540 KB |
Output is correct |
8 |
Correct |
269 ms |
12624 KB |
Output is correct |
9 |
Correct |
401 ms |
11848 KB |
Output is correct |
10 |
Correct |
225 ms |
12624 KB |
Output is correct |
11 |
Correct |
431 ms |
15640 KB |
Output is correct |
12 |
Correct |
237 ms |
18768 KB |
Output is correct |
13 |
Correct |
349 ms |
18780 KB |
Output is correct |
14 |
Correct |
266 ms |
18700 KB |
Output is correct |
15 |
Correct |
28 ms |
3160 KB |
Output is correct |
16 |
Correct |
254 ms |
18832 KB |
Output is correct |
17 |
Correct |
284 ms |
17996 KB |
Output is correct |
18 |
Correct |
307 ms |
13392 KB |
Output is correct |
19 |
Correct |
246 ms |
18768 KB |
Output is correct |
20 |
Correct |
178 ms |
14932 KB |
Output is correct |
21 |
Correct |
13 ms |
1372 KB |
Output is correct |
22 |
Correct |
362 ms |
13680 KB |
Output is correct |
23 |
Correct |
360 ms |
13904 KB |
Output is correct |
24 |
Correct |
340 ms |
14604 KB |
Output is correct |
25 |
Correct |
523 ms |
15940 KB |
Output is correct |
26 |
Correct |
392 ms |
13812 KB |
Output is correct |
27 |
Correct |
47 ms |
4540 KB |
Output is correct |
28 |
Correct |
282 ms |
14932 KB |
Output is correct |
29 |
Correct |
412 ms |
14512 KB |
Output is correct |
30 |
Correct |
457 ms |
16328 KB |
Output is correct |
31 |
Correct |
255 ms |
18772 KB |
Output is correct |
32 |
Correct |
377 ms |
19540 KB |
Output is correct |
33 |
Correct |
410 ms |
15636 KB |
Output is correct |
34 |
Correct |
419 ms |
17028 KB |
Output is correct |
35 |
Correct |
492 ms |
14828 KB |
Output is correct |