結果

問題 No.20 砂漠のオアシス
ユーザー ty70ty70
提出日時 2015-06-22 00:58:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 2,740 bytes
コンパイル時間 724 ms
コンパイル使用メモリ 94,664 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 06:33:06
合計ジャッジ時間 1,535 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF (10*505*505)

/*
	No.20 砂漠のオアシス

	SPFA

	幅優先探索とか、ダイクストラ法と呼ぶにはちょっと違う格子点平面上の最短経路探索。

	幅優先探索では queue を使うが SPFA では priority_queue を使うのが
	一般的。

	幅優先探索でも、解くことができるが O(N^2) とはならず、データ依存?
	SPFA は O(N^2 log N ) かな?
*/

using namespace std;

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

const int MAX_N = 505;

const int dr[] = {-1, 0, 1, 0 };
const int dc[] = { 0, 1, 0,-1 };

int d[MAX_N][MAX_N];
int desert[MAX_N][MAX_N];

int N, V;
int spfa (int sy, int sx, int ey, int ex  ){
	rep (i, N ) rep (j, N ) d[i][j] = INF;
	d[sy][sx] = 0;
	priority_queue<P, vector<P>, greater<P> > que;
	que.push (P (0, sy*N + sx ) );

	while (!que.empty() ){
		P curr = que.top(); que.pop();
		int cost = curr.first;
		int cr = curr.second/N;
		int cc = curr.second%N;
		rep (k, 4 ){
			int nr = cr + dr[k];
			int nc = cc + dc[k];
			int ncost = cost + desert[nr][nc];
			if (nr < 0 || nr >= N || nc < 0 || nc >= N ) continue;
			if (d[nr][nc] > ncost ){
				que.push ( P (ncost, nr*N + nc ) );
				d[nr][nc] = ncost;
			} // end if
		} // end rep
	} // end whlie

	return d[ey][ex];
}

void disp (void ){
	rep (i, N ){
		rep (j, N ){
			cerr << setw(2) << (d[i][j] == INF ? -1 : d[i][j] );
		} // end rep
		cerr << endl;
	} // end rep
	cerr << endl;
}
	
int main()
{
	ios_base::sync_with_stdio(0);
	int Ox, Oy; cin >> N >> V >> Ox >> Oy;
	Ox--, Oy--;

	rep (i, N ) rep (j, N ) cin >> desert[i][j];

	// オアシスがなくても行き着く?
	int ans = spfa (0, 0, N - 1, N - 1 );
	bool res = (ans < V );

//	disp ();
	if (!res && Ox != -1 && Ox != -1 ){
		ans = spfa (0, 0, Oy, Ox );
		if (ans < V ){
			V = 2*(V - d[Oy][Ox] );
			ans = spfa (Oy, Ox, N - 1, N - 1 );
			res |= (ans < V ); 
		} // end if
	} // end if

	cout << (res ? "YES" : "NO" ) << endl;

	return 0;
}
0