結果

提出番号 588
提出者 olphe
言語 C++
提出日時 2017-07-26 00:49:30
問題名 (44)玉ねぎの収穫をするkotamanegi
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 76ms 27872KB
2 AC 100% 55ms 26416KB
3 AC 100% 136ms 27856KB
4 AC 100% 61ms 27872KB
5 AC 100% 70ms 27856KB
6 AC 100% 55ms 30752KB
7 AC 100% 5ms 24864KB
8 AC 100% 155ms 30752KB
9 AC 100% 58ms 27856KB
10 AC 100% 22ms 26432KB
11 AC 100% 18ms 26416KB
12 AC 100% 22ms 24992KB
13 AC 100% 123ms 30752KB
14 AC 100% 6ms 25424KB
15 AC 100% 144ms 30736KB
16 AC 100% 18ms 25008KB
17 AC 100% 42ms 27872KB
18 AC 100% 65ms 27872KB
19 AC 100% 38ms 30752KB
20 AC 100% 15ms 24880KB

ソースコード

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;

const int N = 20;

int main() {
	//std::random_device rnd;	// 乱数生成機
	//std::mt19937 mt(rnd());
	//FILE *ifile[N];
	//FILE *ofile[N];
	//
	//for (int i = 0; i < N; i++) {
	//	char s[100] = {};
	//	char t[100] = {};
	//	sprintf(s, "input/%d.txt", i + 1);
	//	ifile[i] = fopen(s, "w");
	//	sprintf(t, "output/%d.txt", i + 1);
	//	ofile[i] = fopen(t, "w");
	//}
//	for (int i = 0; i < N; i++) {
		int H, W;
		cin >> H >> W;
		int y, x;
		cin >> y >> x;
		int cost_y[1001] = {};
		int cost_x[1001] = {};
		for (int j = 1; j <= H; j++)cin >> cost_y[j];
		for (int j = 1; j <= W; j++)cin >> cost_x[j];
		int yl, yr, xl, xr;
		cin >> yl >> yr >> xl >> xr;
		int dis[1002][1002] = {};
		bool flag[1002][1002] = {};
		for (int j = 1; j <= H; j++) {
			for (int k = 1; k <= W; k++) {
				dis[j][k] = INT_MAX;
				flag[j][k] = true;
			}
		}
		for (int j = yl; j <= yr; j++) {
			for (int k = xl; k <= xr; k++) {
				flag[j][k] = false;
			}
		}
		priority_queue < pair<int, pair<int, int>>, vector < pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>>Q;
		Q.push({ 0,{ 1,1 } });
		while (!Q.empty()) {
			int cy, cx, c;
			c = Q.top().first;
			cy = Q.top().second.first;
			cx = Q.top().second.second;
			Q.pop();
			if (dis[cy][cx] <= c)continue;
			dis[cy][cx] = c;
		//	cout << cy << " " << cx << " " << c << endl;
			if (cy == y&&cx == x)break;
			if (flag[cy + 1][cx]) {
				if (c + cost_x[cx] < dis[cy + 1][cx])Q.push({ c + cost_x[cx] ,{cy + 1,cx} });
			}
			if (flag[cy - 1][cx]) {
				if (c + cost_x[cx] < dis[cy - 1][cx])Q.push({ c + cost_x[cx] ,{ cy - 1,cx } });
			}
			if (flag[cy][cx + 1]) {
				if (c + cost_y[cy] < dis[cy][cx + 1])Q.push({ c + cost_y[cy] ,{ cy,cx + 1 } });
			}
			if (flag[cy][cx - 1]) {
				if (c + cost_y[cy] < dis[cy][cx - 1])Q.push({ c + cost_y[cy] ,{ cy,cx - 1 } });
			}
		}
		if (dis[y][x] == INT_MAX)cout << "-1\n";
		else cout << dis[y][x] << endl;
//	}
	return 0;
}