結果

提出番号 898
提出者 kotatsugame
言語 C++
提出日時 2017-08-01 15:36:40
問題名 (44)玉ねぎの収穫をするkotamanegi
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 31ms 11072KB
2 AC 100% 24ms 8480KB
3 AC 100% 43ms 12656KB
4 AC 100% 35ms 17168KB
5 AC 100% 27ms 10656KB
6 AC 100% 111ms 19248KB
7 AC 100% 4ms 7664KB
8 AC 100% 80ms 10896KB
9 AC 100% 69ms 18304KB
10 AC 100% 39ms 12192KB
11 AC 100% 44ms 12832KB
12 AC 100% 8ms 16144KB
13 AC 100% 106ms 11472KB
14 AC 100% 23ms 12080KB
15 AC 100% 156ms 19968KB
16 AC 100% 6ms 8320KB
17 AC 100% 44ms 13536KB
18 AC 100% 42ms 15504KB
19 AC 100% 47ms 10016KB
20 AC 100% 4ms 9984KB

ソースコード

#include<iostream>
#include<queue>
using namespace std;
int h,w,dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int x[1001],y[1001];
int gx,gy;
int yl,yr,xl,xr;
int d[1001][1001];
main()
{
	cin>>h>>w>>gx>>gy;
	for(int i=0;i++<h;)cin>>x[i];
	for(int i=0;i++<w;)cin>>y[i];
	cin>>xl>>xr>>yl>>yr;
	for(int i=0;i++<h;)for(int j=0;j++<w;)d[i][j]=1e9;
	d[1][1]=0;
	queue<pair<int,int> >P;
	P.push(make_pair(1,1));
	while(!P.empty())
	{
		int nx=P.front().first,ny=P.front().second;
		P.pop();
		for(int r=0;r<4;r++)
		{
			int tx=nx+dx[r],ty=ny+dy[r];
			if(tx<=0||ty<=0||tx>h||ty>w)continue;
			if(xl<=tx&&tx<=xr&&yl<=ty&&ty<=yr)continue;
			int cost=d[nx][ny]+(r%2?x[nx]:y[ny]);
			if(d[tx][ty]>cost)
			{
				d[tx][ty]=cost;
				P.push(make_pair(tx,ty));
			}
		}
	}
	if(d[gx][gy]<1e9)cout<<d[gx][gy]<<endl;
	else cout<<-1<<endl;
}