#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];
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) ans[i][j] = 1e18;
ans[players[0].ff][players[0].ss] = 0;
priority_queue<pair<ll,pii>> pq;
pq.push({0,players[0]});
rep(i,h+1) rep(j,w+1) odw[i][j] = 0;
while(!pq.empty())
{
pair<ll,pii> t = pq.top();
pq.pop();
int x = t.ss.ff;
int y = t.ss.ss;
if(odw[x][y]) continue;
odw[x][y] = 1;
t.ff *= -1;
forall(d,dir)
{
rep2(i,1,max(h,w))
{
if(x + d.ff * i >= 0 && x+d.ff * i <= h && y+d.ss*i >= 0 && y+d.ss*i <= w)
{
ll new_d = min(dist[x+d.ff*i][y+d.ss*i] + t.ff + b + i*a,t.ff + c*i);
if(!(ans[x+d.ff*i][y+d.ss*i] > new_d)) continue;
ans[x+d.ff*i][y+d.ss*i] = new_d;
pq.push({-ans[x+d.ff*i][y+d.ss*i],{x+d.ff*i,y+d.ss*i}});
}
else break;
}
}
}
// 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] << "\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... |