#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
ll dp[501][501];
ll dist[501][501];
ll ans[501][501][5];
bool ball_odw[501][501][5];
bool odw[501][501];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
int h,w,n;
ll a,b,c;
cin >> h >> w >> a >> b >> c >> n;
vector<pii> players;
rep(i,n)
{
int x,y;
cin >> x >> y;
players.pb({x,y});
}
queue<pair<int,pii>> q;
rep(i,n-1) q.push({0,players[i]});
vector<pii> dir = {{-1,0},{1,0},{0,-1},{0,1}};
while(!q.empty())
{
pair<int,pii> t = q.front();
q.pop();
if(odw[t.ss.ff][t.ss.ss]) continue;
odw[t.ss.ff][t.ss.ss] = 1;
dist[t.ss.ff][t.ss.ss] = t.ff;
forall(d,dir)
{
if(t.ss.ff + d.ff >= 0 && t.ss.ff <= max(h,w) && t.ss.ss + d.ff >= 0 && t.ss.ss <= max(h,w))
{
q.push({t.ff+c,{t.ss.ff+d.ff,t.ss.ss+d.ss}});
}
}
}
// rep2(i,0,h)
// {
// rep2(j,0,w)
// {
// cout << dist[i][j] << " ";
// }
// cout << "\n";
// }
dist[players[n-1].ff][players[n-1].ss] = 0;
rep(i,h+1) rep(j,w+1) rep(k,5) ans[i][j][k] = 1e18;
ans[players[0].ff][players[0].ss][4] = 0;
priority_queue<pair<pll,pii>> pq;
pq.push({{0,4},players[0]});
rep(i,h+1) rep(j,w+1) odw[i][j] = 0;
while(!pq.empty())
{
pair<pll,pii> t = pq.top();
pq.pop();
int x = t.ss.ff;
int y = t.ss.ss;
int type = t.ff.ss;
ll odl = -t.ff.ff;
if(ball_odw[x][y][type]) continue;
ball_odw[x][y][type] = 1;
if(type == 4)
{
rep(d,4)
{
if(x + dir[d].ff >= 0 && x+dir[d].ff <= h && y+dir[d].ss >= 0 && y+dir[d].ss <= w)
{
ll move_ans = b + a + odl;
int new_x = x+dir[d].ff;
int new_y = y+dir[d].ss;
if(ans[new_x][new_y][d] > move_ans)
{
ans[new_x][new_y][d] = move_ans;
pq.push({{-move_ans,d},{new_x,new_y}});
}
ll walk_ans = odl+c;
if(ans[new_x][new_y][4] > walk_ans)
{
ans[new_x][new_y][4] = walk_ans;
pq.push({{-walk_ans,4},{new_x,new_y}});
}
}
}
}
else
{
if(ans[x][y][4] > odl + dist[x][y])
{
ans[x][y][4] = odl+dist[x][y];
pq.push({{-(odl+dist[x][y]),4},{x,y}});
}
int new_x = x+dir[type].ff;
int new_y = y+dir[type].ss;
if(x+dir[type].ff >= 0 && x + dir[type].ff <= h && y + dir[type].ss >= 0 && y + dir[type].ss <= w)
{
if(ans[new_x][new_y][type] > odl + a)
{
ans[new_x][new_y][type] = odl+a;
pq.push({{-(odl+a),type},{new_x,new_y}});
}
}
}
}
// cout << "\n";
// rep2(i,0,h)
// {
// rep2(j,0,w)
// {
// cout << ans[i][j] << " ";
// }
// cout << "\n";
// }
cout << ans[players[n-1].ff][players[n-1].ss][4] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |