#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
//#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
ll n, m, k, A, B, C;
void solve(){
ll i, x, y, type;
cin>>n>>m;
cin>>A>>B>>C;
cin>>k;
++n;
++m;
vector<pair<ll, ll>> a(k);
vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(m, vector<ll>(6, inf)));
vector<vector<ll>> dis(n, vector<ll>(m, inf));
set<pair<ll, pair<pair<ll, ll>, ll>>> s;
queue<pair<ll, ll>> q;
for(i=0;i<k;++i){
cin>>a[i].first>>a[i].second;
dis[a[i].first][a[i].second] = 0;
q.push(a[i]);
}
while(!q.empty()){
x = q.front().first;
y = q.front().second;
q.pop();
if(x > 0 and dis[x-1][y] > dis[x][y] + 1){
dis[x-1][y] = dis[x][y] + 1;
q.push({x-1, y});
}
if(x < n - 1 and dis[x+1][y] > dis[x][y] + 1){
dis[x+1][y] = dis[x][y] + 1;
q.push({x+1, y});
}
if(y > 0 and dis[x][y-1] > dis[x][y] + 1){
dis[x][y-1] = dis[x][y] + 1;
q.push({x, y-1});
}
if(y < m-1 and dis[x][y+1] > dis[x][y] + 1){
dis[x][y+1] = dis[x][y] + 1;
q.push({x, y+1});
}
}
dp[a[0].first][a[0].second][0] = 0;
s.insert({0, {{a[0].first, a[0].second}, 0}});
while(!s.empty()){
x = s.begin()->second.first.first;
y = s.begin()->second.first.second;
type = s.begin()->second.second;
s.erase(s.begin());
if(type == 0){
if(dp[x][y][1] > dp[x][y][0] + dis[x][y]*C){
s.erase({dp[x][y][1], {{x, y}, 1}});
dp[x][y][1] = dp[x][y][0] + dis[x][y]*C;
s.insert({dp[x][y][1], {{x, y}, 1}});
}
}else if(type == 1){
for(i=2;i<6;++i){
if(dp[x][y][i] > dp[x][y][1] + B){
s.erase({dp[x][y][i], {{x, y}, i}});
dp[x][y][i] = dp[x][y][1] + B;
s.insert({dp[x][y][i], {{x, y}, i}});
}
}
if(x < n-1 and dp[x+1][y][type] > dp[x][y][type] + C){
s.erase({dp[x+1][y][type], {{x+1, y}, type}});
dp[x+1][y][type] = dp[x][y][type] + C;
s.insert({dp[x+1][y][type], {{x+1, y}, type}});
}
if(x > 0 and dp[x-1][y][type] > dp[x][y][type] + C){
s.erase({dp[x-1][y][type], {{x-1, y}, type}});
dp[x-1][y][type] = dp[x][y][type] + C;
s.insert({dp[x-1][y][type], {{x-1, y}, type}});
}
if(y < m-1 and dp[x][y+1][type] > dp[x][y][type] + C){
s.erase({dp[x][y+1][type], {{x, y+1}, type}});
dp[x][y+1][type] = dp[x][y][type] + C;
s.insert({dp[x][y+1][type], {{x, y+1}, type}});
}
if(y > 0 and dp[x][y-1][type] > dp[x][y][type] + C){
s.erase({dp[x][y-1][type], {{x, y-1}, type}});
dp[x][y-1][type] = dp[x][y][type] + C;
s.insert({dp[x][y-1][type], {{x, y-1}, type}});
}
}else{
if(dp[x][y][0] > dp[x][y][type]){
s.erase({dp[x][y][0], {{x, y}, 0}});
dp[x][y][0] = dp[x][y][type];
s.insert({dp[x][y][0], {{x, y}, 0}});
}
if(type == 2){
if(x < n-1 and dp[x+1][y][type] > dp[x][y][type] + A){
s.erase({dp[x+1][y][type], {{x+1, y}, type}});
dp[x+1][y][type] = dp[x][y][type] + A;
s.insert({dp[x+1][y][type], {{x+1, y}, type}});
}
}
if(type == 3){
if(x > 0 and dp[x-1][y][type] > dp[x][y][type] + A){
s.erase({dp[x-1][y][type], {{x-1, y}, type}});
dp[x-1][y][type] = dp[x][y][type] + A;
s.insert({dp[x-1][y][type], {{x-1, y}, type}});
}
}
if(type == 4){
if(y < m-1 and dp[x][y+1][type] > dp[x][y][type] + A){
s.erase({dp[x][y+1][type], {{x, y+1}, type}});
dp[x][y+1][type] = dp[x][y][type] + A;
s.insert({dp[x][y+1][type], {{x, y+1}, type}});
}
}
if(type == 5){
if(y > 0 and dp[x][y-1][type] > dp[x][y][type] + A){
s.erase({dp[x][y-1][type], {{x, y-1}, type}});
dp[x][y-1][type] = dp[x][y][type] + A;
s.insert({dp[x][y-1][type], {{x, y-1}, type}});
}
}
}
}
cout<<min(dp[a.back().first][a.back().second][0], dp[a.back().first][a.back().second][1])<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
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... |