/*
JOI 2017 Soccer
- First, find the minimum distance to any player for each coordinate
- Notice how the game will look like
- Run a few steps
- Kick the ball
- Another player goes to the ball
- Repeat
- Then we just do Dijkstra
- Complexity: O(H^3)
*/
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
struct Ball {
int x, y, d;
ll f;
};
bool operator<(Ball A, Ball B) { return A.f > B.f; }
int h, w;
ll vert[501][501], to_p[501][501], dp[501][501][6];
bool visited[501][501][6];
int dx[6]{0, 0, 0, 0, 1, -1}, dy[6]{0, 0, 1, -1, 0, 0};
bool inside(int x, int y) { return (~x && ~y && x <= h && y <= w); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
ll a, b, c;
Ball dest;
priority_queue<Ball> pq;
cin >> h >> w >> a >> b >> c >> n;
memset(vert, 0x3f, sizeof(vert));
memset(to_p, 0x3f, sizeof(to_p));
memset(dp, 0x3f, sizeof(dp));
FOR(i, 0, n) {
int x, y;
cin >> x >> y;
vert[x][y] = 0;
if (!i) pq.push({x, y, 1, 0}), dp[x][y][1] = 0;
if (i == n - 1) dest = {x, y, 0, 0};
}
FOR(i, 0, h + 1) FOR(j, 0, w + 1) {
if (i) {
vert[i][j] = min(vert[i][j], vert[i - 1][j] + 1);
vert[h - i][j] = min(vert[h - i][j], vert[h - i + 1][j] + 1);
}
}
FOR(i, 0, h + 1) FOR(j, 0, w + 1) FOR(k, 0, w + 1) {
to_p[i][j] = min(to_p[i][j], vert[i][k] + abs(j - k));
}
while (pq.size()) {
Ball curr = pq.top();
pq.pop();
if (!visited[curr.x][curr.y][curr.d]) {
visited[curr.x][curr.y][curr.d] = true;
// Ball is at (x, y)
if (!curr.d) { // Nearest player runs to ball
if (curr.f + to_p[curr.x][curr.y] * c < dp[curr.x][curr.y][1]) {
dp[curr.x][curr.y][1] = curr.f + to_p[curr.x][curr.y] * c;
pq.push({curr.x, curr.y, 1, curr.f + to_p[curr.x][curr.y] * c});
}
} else if (curr.d == 1) { // Player has ball
FOR(i, 2, 6) {
// Player runs in direction i with the ball
int nx = curr.x + dx[i], ny = curr.y + dy[i];
if (inside(nx, ny) && curr.f + c < dp[nx][ny][1]) {
dp[nx][ny][1] = curr.f + c;
pq.push({nx, ny, 1, curr.f + c});
}
// Player kicks the ball in direction i
if (curr.f + b < dp[curr.x][curr.y][i]) {
dp[curr.x][curr.y][i] = curr.f + b;
pq.push({curr.x, curr.y, i, curr.f + b});
}
}
} else { // Ball is/was moving in direction d
// Ball continues in the direction
int nx = curr.x + dx[curr.d], ny = curr.y + dy[curr.d];
if (inside(nx, ny) && curr.f + a < dp[nx][ny][curr.d]) {
dp[nx][ny][curr.d] = curr.f + a;
pq.push({nx, ny, curr.d, curr.f + a});
}
// Ball stops
if (curr.f < dp[curr.x][curr.y][0]) {
dp[curr.x][curr.y][0] = curr.f;
pq.push({curr.x, curr.y, 0, curr.f});
}
}
}
}
ll ans = LLONG_MAX;
FOR(i, 0, 6) ans = min(ans, dp[dest.x][dest.y][i]);
cout << ans;
return 0;
}
Compilation message
soccer.cpp: In function 'int main()':
soccer.cpp:37:10: warning: 'dest.Ball::x' may be used uninitialized in this function [-Wmaybe-uninitialized]
Ball dest;
^~~~
soccer.cpp:37:10: warning: 'dest.Ball::y' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
18672 KB |
Output is correct |
2 |
Correct |
13 ms |
16128 KB |
Output is correct |
3 |
Correct |
465 ms |
23648 KB |
Output is correct |
4 |
Correct |
492 ms |
23936 KB |
Output is correct |
5 |
Correct |
100 ms |
17144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
494 ms |
23908 KB |
Output is correct |
2 |
Correct |
499 ms |
23780 KB |
Output is correct |
3 |
Correct |
382 ms |
23524 KB |
Output is correct |
4 |
Correct |
430 ms |
24040 KB |
Output is correct |
5 |
Correct |
372 ms |
23784 KB |
Output is correct |
6 |
Correct |
461 ms |
23872 KB |
Output is correct |
7 |
Correct |
505 ms |
23780 KB |
Output is correct |
8 |
Correct |
491 ms |
23780 KB |
Output is correct |
9 |
Correct |
517 ms |
23900 KB |
Output is correct |
10 |
Correct |
62 ms |
19192 KB |
Output is correct |
11 |
Correct |
501 ms |
23780 KB |
Output is correct |
12 |
Correct |
510 ms |
23908 KB |
Output is correct |
13 |
Correct |
347 ms |
23524 KB |
Output is correct |
14 |
Correct |
487 ms |
23904 KB |
Output is correct |
15 |
Correct |
370 ms |
24032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
18672 KB |
Output is correct |
2 |
Correct |
13 ms |
16128 KB |
Output is correct |
3 |
Correct |
465 ms |
23648 KB |
Output is correct |
4 |
Correct |
492 ms |
23936 KB |
Output is correct |
5 |
Correct |
100 ms |
17144 KB |
Output is correct |
6 |
Correct |
494 ms |
23908 KB |
Output is correct |
7 |
Correct |
499 ms |
23780 KB |
Output is correct |
8 |
Correct |
382 ms |
23524 KB |
Output is correct |
9 |
Correct |
430 ms |
24040 KB |
Output is correct |
10 |
Correct |
372 ms |
23784 KB |
Output is correct |
11 |
Correct |
461 ms |
23872 KB |
Output is correct |
12 |
Correct |
505 ms |
23780 KB |
Output is correct |
13 |
Correct |
491 ms |
23780 KB |
Output is correct |
14 |
Correct |
517 ms |
23900 KB |
Output is correct |
15 |
Correct |
62 ms |
19192 KB |
Output is correct |
16 |
Correct |
501 ms |
23780 KB |
Output is correct |
17 |
Correct |
510 ms |
23908 KB |
Output is correct |
18 |
Correct |
347 ms |
23524 KB |
Output is correct |
19 |
Correct |
487 ms |
23904 KB |
Output is correct |
20 |
Correct |
370 ms |
24032 KB |
Output is correct |
21 |
Correct |
126 ms |
17396 KB |
Output is correct |
22 |
Correct |
607 ms |
23776 KB |
Output is correct |
23 |
Correct |
549 ms |
20812 KB |
Output is correct |
24 |
Correct |
617 ms |
20712 KB |
Output is correct |
25 |
Correct |
561 ms |
23908 KB |
Output is correct |
26 |
Correct |
578 ms |
23904 KB |
Output is correct |
27 |
Correct |
412 ms |
18360 KB |
Output is correct |
28 |
Correct |
400 ms |
18472 KB |
Output is correct |
29 |
Correct |
537 ms |
21356 KB |
Output is correct |
30 |
Correct |
380 ms |
18296 KB |
Output is correct |
31 |
Correct |
545 ms |
23904 KB |
Output is correct |
32 |
Correct |
620 ms |
24804 KB |
Output is correct |
33 |
Correct |
479 ms |
23776 KB |
Output is correct |
34 |
Correct |
627 ms |
23956 KB |
Output is correct |
35 |
Correct |
329 ms |
18424 KB |
Output is correct |