結果

問題 No.20 砂漠のオアシス
ユーザー tkzw_21tkzw_21
提出日時 2015-03-09 23:36:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,812 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 174,708 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 06:24:57
合計ジャッジ時間 2,571 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 16 ms
6,940 KB
testcase_06 AC 18 ms
6,940 KB
testcase_07 AC 18 ms
6,940 KB
testcase_08 AC 17 ms
6,940 KB
testcase_09 AC 18 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 5 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 5 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;
typedef pair<int,pair<int,int>> PP;
typedef long long ll;

const double EPS = 1e-8;
const int INF = 1e9;
const int MOD = 1e9+7;

int dy[] = {0,1,0,-1};
int dx[] = {1,0,-1,0};

bool bfs(int V,int ox,int oy,vector<vector<int>>L){
	priority_queue<PP,vector<PP>,greater<PP>> pq;
	pq.push(make_pair(0,P(0,0)));
	int n = L.size();
	vector<vector<bool>>used(n,vector<bool>(n));

	int StoO=0,StoG=0;

	while(!pq.empty()){
		PP pp = pq.top();pq.pop();
		int x = pp.second.first;
		int y = pp.second.second;
		int d = pp.first;
		if(x == ox && y == oy)StoO = d;
		if(x == n-1 && y == n-1)StoG = d;
		for(int i=0;i<4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(nx < 0 || ny < 0 || nx >= n || ny >= n)continue;
			if(used[ny][nx])continue;
			used[ny][nx] = true;
			pq.push(make_pair(d+L[ny][nx],P(nx,ny)));
		}
	}

	pq.push(make_pair(0,P(ox,oy)));
	vector<vector<bool>>used2(n,vector<bool>(n));

	int OtoG=0;
	while(!pq.empty()){
		PP pp = pq.top();pq.pop();
		int x = pp.second.first;
		int y = pp.second.second;
		int d = pp.first;
		if(x == n-1 && y == n-1)OtoG = d;
		for(int i=0;i<4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(nx < 0 || ny < 0 || nx >= n || ny >= n)continue;
			if(used2[ny][nx])continue;
			used2[ny][nx] = true;
			pq.push(make_pair(d+L[ny][nx],P(nx,ny)));
		}
	}
	//cout << StoG << " " << StoO << " " << OtoG << endl;
	if(StoG < V)return true;
	if(ox+oy>=0 && V-StoO > 0){
		V = (V-StoO)*2;
		if(OtoG < V)return true;
	}
	return false;
}


int main(void) {
	int n,v,ox,oy;
	cin >> n >> v >> ox >> oy;
	ox--;oy--;
	vector<vector<int>>L(n,vector<int>(n));
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> L[i][j];
		}
	}
	if(bfs(v,ox,oy,L))cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}

0