#include <bits/stdc++.h>
using namespace std;
template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}
#define all(a) a.begin(), a.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
const int MAX_N = 1e5 + 5;
const int MAX_M = 500 + 5;
const ll inf = 1e18 + 1;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int h, w;
int n;
int A, B, C;
pii s[MAX_N];
int nearestPlayer[MAX_M][MAX_M];
int ball[MAX_M][MAX_M][4];
int player[MAX_M][MAX_M];
queue<pii> q;
bool inside(int x, int y) {
return ~x && ~y && x <= h && y <= w;
}
bool inside(pii x) {
return ~x.fi && ~x.se && x.fi <= h && x.se <= w;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w >> A >> B >> C >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i].fi >> s[i].se;
nearestPlayer[s[i].fi][s[i].se] = 1;
q.push(s[i]);
}
while (!q.empty()) {
pii u = q.front();
q.pop();
for (int k = 0; k < 4; k++) {
pii v = {u.fi + dx[k], u.se + dy[k]};
if (inside(v) && !nearestPlayer[v.fi][v.se]) {
nearestPlayer[v.fi][v.se] = nearestPlayer[u.fi][u.se] + 1;
q.push(v);
}
}
}
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= w; j++) {
nearestPlayer[i][j]--;
nearestPlayer[i][j] *= C;
player[i][j] = inf;
for (int k = 0; k < 4; k++) {
ball[i][j][k] = inf;
}
}
}
priority_queue<pair<int, pair<pii, int>>, vector<pair<int, pair<pii, int>>>, greater<pair<int, pair<pii, int>>>> pq;
player[s[1].fi][s[1].se] = 0;
pq.push({0, {s[1], 4}});
while (!pq.empty()) {
int currW = pq.top().fi;
pii u = pq.top().se.fi;
int dir = pq.top().se.se;
pq.pop();
if (dir == 4) {
if (currW > player[u.fi][u.se]) {
continue;
}
for (int k = 0; k < 4; k++) {
if (currW + B < ball[u.fi][u.se][k]) {
ball[u.fi][u.se][k] = currW + B;
pq.push({ball[u.fi][u.se][k], {u, k}});
}
pii v = {u.fi + dx[k], u.se + dy[k]};
if (!inside(v)) continue;
if (currW + C < player[v.fi][v.se]) {
player[v.fi][v.se] = currW + C;
pq.push({player[v.fi][v.se], {v, 4}});
}
}
}
else {
if (currW > ball[u.fi][u.se][dir]) {
continue;
}
pii v = {u.fi + dx[dir], u.se + dy[dir]};
if (inside(v)) {
if (currW + A < ball[v.fi][v.se][dir]) {
ball[v.fi][v.se][dir] = currW + A;
pq.push({ball[v.fi][v.se][dir], {v, dir}});
}
}
if (currW + nearestPlayer[u.fi][u.se] < player[u.fi][u.se]) {
player[u.fi][u.se] = currW + nearestPlayer[u.fi][u.se];
pq.push({player[u.fi][u.se], {u, 4}});
}
}
}
int ans = inf;
for (int k = 0; k < 4; k++) {
minimize(ans, ball[s[n].fi][s[n].se][k]);
}
minimize(ans, player[s[n].fi][s[n].se]);
cout << ans;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
8012 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
254 ms |
20528 KB |
Output is correct |
4 |
Correct |
276 ms |
20484 KB |
Output is correct |
5 |
Correct |
62 ms |
7520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
20488 KB |
Output is correct |
2 |
Correct |
280 ms |
20544 KB |
Output is correct |
3 |
Correct |
213 ms |
18320 KB |
Output is correct |
4 |
Correct |
199 ms |
20508 KB |
Output is correct |
5 |
Correct |
202 ms |
20588 KB |
Output is correct |
6 |
Correct |
226 ms |
20540 KB |
Output is correct |
7 |
Correct |
263 ms |
20660 KB |
Output is correct |
8 |
Correct |
255 ms |
20552 KB |
Output is correct |
9 |
Correct |
306 ms |
20688 KB |
Output is correct |
10 |
Correct |
47 ms |
9792 KB |
Output is correct |
11 |
Correct |
266 ms |
20784 KB |
Output is correct |
12 |
Correct |
271 ms |
20664 KB |
Output is correct |
13 |
Correct |
161 ms |
18236 KB |
Output is correct |
14 |
Correct |
279 ms |
20736 KB |
Output is correct |
15 |
Correct |
207 ms |
20596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
8012 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
254 ms |
20528 KB |
Output is correct |
4 |
Correct |
276 ms |
20484 KB |
Output is correct |
5 |
Correct |
62 ms |
7520 KB |
Output is correct |
6 |
Correct |
285 ms |
20488 KB |
Output is correct |
7 |
Correct |
280 ms |
20544 KB |
Output is correct |
8 |
Correct |
213 ms |
18320 KB |
Output is correct |
9 |
Correct |
199 ms |
20508 KB |
Output is correct |
10 |
Correct |
202 ms |
20588 KB |
Output is correct |
11 |
Correct |
226 ms |
20540 KB |
Output is correct |
12 |
Correct |
263 ms |
20660 KB |
Output is correct |
13 |
Correct |
255 ms |
20552 KB |
Output is correct |
14 |
Correct |
306 ms |
20688 KB |
Output is correct |
15 |
Correct |
47 ms |
9792 KB |
Output is correct |
16 |
Correct |
266 ms |
20784 KB |
Output is correct |
17 |
Correct |
271 ms |
20664 KB |
Output is correct |
18 |
Correct |
161 ms |
18236 KB |
Output is correct |
19 |
Correct |
279 ms |
20736 KB |
Output is correct |
20 |
Correct |
207 ms |
20596 KB |
Output is correct |
21 |
Correct |
68 ms |
7396 KB |
Output is correct |
22 |
Correct |
335 ms |
20548 KB |
Output is correct |
23 |
Correct |
296 ms |
15336 KB |
Output is correct |
24 |
Correct |
379 ms |
16740 KB |
Output is correct |
25 |
Correct |
278 ms |
20692 KB |
Output is correct |
26 |
Correct |
351 ms |
21076 KB |
Output is correct |
27 |
Correct |
214 ms |
15816 KB |
Output is correct |
28 |
Correct |
198 ms |
16448 KB |
Output is correct |
29 |
Correct |
284 ms |
20236 KB |
Output is correct |
30 |
Correct |
190 ms |
16268 KB |
Output is correct |
31 |
Correct |
290 ms |
20844 KB |
Output is correct |
32 |
Correct |
355 ms |
24436 KB |
Output is correct |
33 |
Correct |
236 ms |
20552 KB |
Output is correct |
34 |
Correct |
342 ms |
20808 KB |
Output is correct |
35 |
Correct |
163 ms |
16220 KB |
Output is correct |