ソースコード
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
bool inside(int a, int b, int alim, int blim){
return 0 <= a && a < alim && 0 <= b && b < blim;
}
bool inside2(int a, int b, int amin, int amax, int bmin, int bmax){
return amin <= a && a <= amax && bmin <= b && b <= bmax;
}
int main(){
int h, w, x, y;
cin >> h >> w >> x >> y;
pair<int, int> goal(--x, --y);
int a[h], b[w];
for(int i = 0; i < h; i++) cin >> a[i];
for(int i = 0; i < w; i++) cin >> b[i];
int c, d, e, f;
cin >> c >> d >> e >> f;
c--; d--; e--; f--;
priority_queue<pair<int64_t, pair<int, int>>, vector<pair<int64_t, pair<int, int>>>, greater<pair<int64_t, pair<int, int>>>> pq;
pq.push(make_pair(0, make_pair(0, 0)));
int dd[] = {0, 1, 0, -1, 0};
bool done[h][w]{};
int64_t score[h][w];
fill(score[0], score[h], 1ll << 60);
score[0][0] = 0;
while(!pq.empty()){
pair<int64_t, pair<int, int>> puni = pq.top();
pq.pop();
int64_t cost = puni.first;
pair<int, int> now = puni.second;
if(done[now.first][now.second]) continue;
done[now.first][now.second] = true;
for(int i = 0; i < 4; i++){
pair<int, int> next(now.first + dd[i], now.second + dd[i + 1]);
if(inside(next.first, next.second, h, w) && !inside2(next.first, next.second, c, d, e, f) && score[now.first][now.second] + (abs(now.first - next.first) ? b[now.second] : a[now.first]) < score[next.first][next.second]){
score[next.first][next.second] = score[now.first][now.second] + (abs(now.first - next.first) ? b[now.second] : a[now.first]);
pq.push(make_pair(score[next.first][next.second], next));
}
}
}
cout << (score[h - 1][w - 1] == 1ll << 60 ? -1 : score[h - 1][w - 1]) << endl;
}