#include <bits/stdc++.h>
using namespace std;
//loops (warning : long long)
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r - 1); i >= l; --i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
//pairs, tuples
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
//vectors
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sum_of(v) accumulate(all(v), 0ll)
#define sz(v) (int)v.size()
#define compact(v) v.erase(unique(all(v)), end(v))
//binary search
#define lwb lower_bound
#define upb upper_bound
//other stuffs
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using db = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll dp[3][501][501];
using node = tuple<ll, int, int, int>;
priority_queue<node, vector<node>, greater<node>> pq;
//dp[state][x][y] : minimum cost for the ball to be in the (x, y)
//state = 0 : no one is currently taking the ball and it's moving horizontally
//state = 1 : no one is currently taking the ball and it's moving vertically
//state = 2 : someone is currently taking the ball, moving whenever he wants
void relax(int state, int x, int y, ll cost){
if(minimize(dp[state][x][y], cost)) pq.push(mt(dp[state][x][y], state, x, y));
}
void testcase(int ntestcase){
int N, M, A, B, C;
cin >> N >> M >> A >> B >> C;
++N, ++M;
vector<vector<int>> dist(N, vector<int>(M, -1)); //minimum distance from a player to position (i, j)
int K;
cin >> K;
int startx, starty;
int ansx, ansy;
queue<pi> q;
for(int i = 0; i < K; ++i){
int x, y;
cin >> x >> y;
if(i == 0) startx = x, starty = y;
if(i + 1 < K){
dist[x][y] = 0;
q.push(mp(x, y));
} else{
ansx = x; ansy = y;
}
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
while(!q.empty()){
int x, y; tie(x, y) = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(0 <= nx && nx < N && 0 <= ny && ny < M && dist[nx][ny] == -1){
dist[nx][ny] = dist[x][y] + 1;
q.push(mp(nx, ny));
}
}
}
// for(int i = 0; i < N; ++i){
// for(int j = 0; j < M; ++j){
// cout << dist[i][j] << ' ';
// }
// cout << '\n';
// }
memset(dp, 0x3f, sizeof(dp));
dp[0][startx][starty] = B;
dp[1][startx][starty] = B;
dp[2][startx][starty] = 0;
for(int i = 0; i < 3; ++i) pq.push(mt(dp[i][startx][starty], i, startx, starty));
while(!pq.empty()){
ll cost; int state, x, y;
tie(cost, state, x, y) = pq.top(); pq.pop();
// cout << cost << ' ' << state << ' ' << x << ' ' << y << '\n';
// assert(state < 3);
// assert(x < N);
// assert(y < M);
if(cost != dp[state][x][y]) continue;
if(state == 0){
//someone kick the ball horizontally, and it keeps moving
if(y > 0) relax(0, x, y-1, cost + A);
if(y < M-1) relax(0, x, y+1, cost + A);
//someone takes the ball at this cell
relax(2, x, y, cost + 1LL * C * dist[x][y]);
} else if(state == 1){
//someone kick the ball vertically, and it keeps moving
if(x > 0) relax(1, x-1, y, cost + A);
if(x < N-1) relax(1, x+1, y, cost + A);
//some takes the ball at this cell
relax(2, x, y, cost + 1LL * C * dist[x][y]);
} else{
//keep moving
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(0 <= nx && nx < N && 0 <= ny && ny < M){
relax(2, nx, ny, cost + C);
}
}
//start shooting somewhere else
relax(0, x, y, cost + B);
relax(1, x, y, cost + B);
}
}
ll ans = LLONG_MAX;
for(int i = 0; i < 3; ++i) minimize(ans, dp[i][ansx][ansy]);
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
#endif // LOCAL
int T = 1;
// cin >> T;
FOR(i, 0, T) testcase(i);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |