#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 5e2 + 5, inf = 1e16;
int dist[maxn][maxn], res[maxn][maxn][5]; //0: up, 1: down, 2: right, 3: left, 4: kept
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int h, w, a, b, c, n; cin >> h >> w >> a >> b >> c >> n;
for(int i = 0; i <= h; i++){
for(int j = 0; j <= w; j++){
dist[i][j] = inf;
for(int k = 0; k < 5; k++) res[i][j][k] = inf;
}
}
int startx, starty, endx, endy;
queue<pair<int, int>> q;
for(int i = 1; i <= n; i++){
int x, y; cin >> x >> y;
if(i == 1) startx = x, starty = y;
if(i == n) endx = x, endy = y;
dist[x][y] = 0;
q.push({x, y});
}
auto inside = [&](int x, int y){
return 0 <= x && x <= h && 0 <= y && y <= w;
};
auto minimize = [&](int &x, int y){
if(x > y){
x = y;
return true;
}return false;
};
while(!q.empty()){
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if (inside(nx, ny) && dist[nx][ny] == inf){
dist[nx][ny] = dist[x][y] + 1;
q.push({nx, ny});
}
}
}
priority_queue<array<int, 4>, vector<array<int, 4>>, greater<array<int, 4>>> pq;
res[startx][starty][4] = 0;
pq.push({0, startx, starty, 4});
while(pq.size()){
auto [d, x, y, m] = pq.top();
pq.pop();
if(d > res[x][y][m]) continue;
if(m == 4){
for(int i = 0; i < 4; i++){
if(minimize(res[x][y][i], d + b)) pq.push({res[x][y][i], x, y, i});
int nx = x + dx[i], ny = y + dy[i];
if(inside(nx, ny) && minimize(res[nx][ny][4], d + c)) pq.push({res[nx][ny][4], nx, ny, 4});
}
}else{
int nx = x + dx[m], ny = y + dy[m];
if(inside(nx, ny) && minimize(res[nx][ny][m], d + a)) pq.push({res[nx][ny][m], nx, ny, m});
if(minimize(res[x][y][4], d + dist[x][y] * c)) pq.push({res[x][y][4], x, y, 4});
}
}
cout << res[endx][endy][4];
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |