結果

提出番号 963
提出者 Yang33
言語 C++
提出日時 2017-08-01 16:32:29
問題名 (44)玉ねぎの収穫をするkotamanegi
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 44ms 19776KB
2 AC 100% 29ms 14000KB
3 AC 100% 77ms 23760KB
4 AC 100% 76ms 32848KB
5 AC 100% 55ms 19760KB
6 AC 100% 269ms 45504KB
7 AC 100% 4ms 8304KB
8 AC 100% 81ms 23088KB
9 AC 100% 66ms 30272KB
10 AC 100% 82ms 25760KB
11 AC 100% 92ms 24080KB
12 AC 100% 13ms 18656KB
13 AC 100% 89ms 21312KB
14 AC 100% 29ms 19056KB
15 AC 100% 240ms 41136KB
16 AC 100% 7ms 8960KB
17 AC 100% 89ms 25504KB
18 AC 100% 71ms 27696KB
19 AC 100% 76ms 21344KB
20 AC 100% 6ms 11408KB

ソースコード

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define FOR(i, s, e) for (ll(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (ll(i) = (s); (i) > (e); (i)--)
#define debug(x) cout << #x << ": " << x << endl
#define mp make_pair
#define pb push_back
const ll MOD = 1000000007;
const int INF = 1e9;
const ll LINF = 1e16;
const double PI = acos(-1.0);
int dx[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };
int dy[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2017/08/01  Problem: KotamanegiOJ_Otemae F  / Link:   ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

ll N;

ll ans = LINF;
ll W, H, Ty, Tx;
int cx[1003];
int cy[1003];
ll yl, yr, xl, xr;
ll masu[1003][1003];

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> H >> W >> Tx >> Ty;
	FOR(i, 1, H + 1) {
		cin >> cx[i];
	}
	FOR(i, 1, W + 1) {
		cin >> cy[i];
	}
	cin >> xl >> xr >> yl >> yr;
	FOR(i, 0, H+1)FOR(j, 0, W+1) {
		masu[i][j] = LINF;
	}
	masu[1][1] = 0;
	using tt = tuple<ll, ll, ll>;
	priority_queue<tt, vector<tt>, greater<tt>>q;
	q.push(tt(0, 1, 1));

	while (!q.empty()) {
		tt a = q.top(); q.pop();
		ll d, x, y;
		tie(d, y, x) = a;
		if (masu[x][y] < d)continue;

		FOR(i, 0, 4) {
			int nx = x + dx[i]; int ny = y + dy[i];
			if (!(1 <= nx&&nx <= H && 1 <= ny&&ny <= W))continue;

			if (xl <= nx && nx <= xr&&yl <= ny && ny <= yr)continue;
			ll nc;
			if (i < 2) {//y
				nc = cx[x];
			}
			else {//x
				nc = cy[y];
			}
			if (masu[nx][ny] > d + nc) {
				masu[nx][ny] = d + nc;
				q.push(tt(masu[nx][ny], ny, nx));
			}
		}
	}

	/*FOR(i, 1, H + 1) {
		FOR(j, 1, W + 1) {
			cout << masu[i][j] << " ";
		}cout << endl;
	}*/
	ans = masu[Tx][Ty];

	if (ans == LINF)ans = -1;
	cout << ans << endl;

	return 0;
}