ソースコード
#include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define int3(x, y, z) Int(x); int2(y, z);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
int dxy[5] = {0, 1, 0, -1, 0};
// assign avl ncm dijkstra geo2 kruskal graph uf lca BIT
// matrix dinic next_combination topcoder
signed main()
{
int2(h, w);
int2(x, y);
vvi board(h+1, vi(w+1, INF));
vi mx(h), my(w);
rep(i, h) {
cin >> mx[i];
}
rep(i, w) {
cin >> my[i];
}
h++; w++;
int2(sx, sy);
int2(gx, gy);
//std::cout << "no" << std::endl;
for (int i = sx; i <= gx; i++) {
for (int j = sy; j <= gy; j++) {
board[i][j] = -1;
}
}
board[1][1] = 0;
queue<pair <int, i_i> > Funami;
Funami.push(mp(0, i_i(1, 1)));
while (!Funami.empty()) {
int cost = Funami.front().fir;
int nx = Funami.front().sfir, ny = Funami.front().ssec;
Funami.pop();
// std::cout << nx << " " << ny << std::endl;
if (board[nx][ny] == cost) {
rep(i, 4) {
int nnx = nx + dxy[i], nny = ny + dxy[i+1];
if (0 < nnx && nnx < h && 0 < nny && nny < w) {
if (!i || i == 2) {
if (board[nnx][nny] > cost + mx[nnx-1]) {
int tmp = cost + mx[nnx-1];
board[nnx][nny] = tmp;
Funami.push(mp(tmp, i_i(nnx, nny)));
}
} else {
if (board[nnx][nny] > cost + my[nny-1]) {
int tmp = cost + my[nny-1];
board[nnx][nny] = tmp;
Funami.push(mp(tmp, i_i(nnx, nny)));
}
}
}
}
}
}
/*
rep(i, h) {
rep(j, w) {
std::cout << board[i][j] << " ";
} cout << endl;
}
*/
if (board[x][y] == INF) {
std::cout << -1 << std::endl;
} else {
std::cout << board[x][y] << std::endl;
}
return 0;
}