ソースコード
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
using namespace std;
#define eps 0.00000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define f(n) for(int i=0;i<n;++i)
#define REP(i,n) for(long long i = 0;i < n;++i)
long long dp[2000][2000] = {};
bool done[2000][2000] = {};
long long y[2000] = {}, x[2000] = {};
int main() {
#define int long long
int h, w;
cin >> h >> w;
REP(i, h) {
REP(q, w) {
dp[i + 1][q + 1] = LONG_INF;
}
}
dp[1][1] = 0;
int y_dis, x_dis;
cin >> x_dis >> y_dis;
REP(i, h) {
cin >> x[i + 1];
}
REP(i, w) {
cin >> y[i + 1];
}
long long a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = a;i <= b;++i) {
for (int q = c;q <= d;++q) {
done[i][q] = 1;
}
}
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> hoge;
hoge.push(make_tuple(0, 1, 1));
while (hoge.empty() == false) {
tuple<int, int, int> wow = hoge.top();hoge.pop();
if (done[get<1>(wow)][get<2>(wow)] == false) {
done[get<1>(wow)][get<2>(wow)] = true;
if (get<1>(wow) < h) {
if (done[get<1>(wow) + 1][get<2>(wow)] == false && dp[get<1>(wow) + 1][get<2>(wow)] > get<0>(wow) + y[get<2>(wow)]) {
dp[get<1>(wow) + 1][get<2>(wow)] = get<0>(wow) + y[get<2>(wow)];
hoge.push(make_tuple(dp[get<1>(wow) + 1][get<2>(wow)], get<1>(wow) + 1, get<2>(wow)));
}
}
if (get<1>(wow) > 1) {
if (done[get<1>(wow) - 1][get<2>(wow)] == false && dp[get<1>(wow) - 1][get<2>(wow)] > get<0>(wow) + y[get<2>(wow)]) {
dp[get<1>(wow) - 1][get<2>(wow)] = get<0>(wow) + y[get<2>(wow)];
hoge.push(make_tuple(dp[get<1>(wow) - 1][get<2>(wow)], get<1>(wow) - 1, get<2>(wow)));
}
}
if (get<2>(wow) < w) {
if (done[get<1>(wow)][get<2>(wow) + 1] == false && dp[get<1>(wow)][get<2>(wow) + 1] > get<0>(wow) + x[get<1>(wow)]) {
dp[get<1>(wow)][get<2>(wow) + 1] = get<0>(wow) + x[get<1>(wow)];
hoge.push(make_tuple(dp[get<1>(wow)][get<2>(wow) + 1], get<1>(wow), get<2>(wow) + 1));
}
}
if (get<2>(wow) > 1) {
if (done[get<1>(wow)][get<2>(wow) - 1] == false && dp[get<1>(wow)][get<2>(wow) - 1] > get<0>(wow) + x[get<1>(wow)]) {
dp[get<1>(wow)][get<2>(wow) - 1] = get<0>(wow) + x[get<1>(wow)];
hoge.push(make_tuple(dp[get<1>(wow)][get<2>(wow) - 1], get<1>(wow), get<2>(wow) - 1));
}
}
}
}
if (dp[x_dis][y_dis] != LONG_INF) {
cout << dp[x_dis][y_dis] << endl;
}
else {
cout << -1 << endl;
}
}