結果

問題 No.20 砂漠のオアシス
ユーザー furafura
提出日時 2020-01-06 19:24:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 1,278 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 175,024 KB
実行使用メモリ 6,604 KB
最終ジャッジ日時 2023-08-14 22:49:45
合計ジャッジ時間 2,839 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
vector<T> Dijkstra(const weighted_graph<T>& G,int s){
	const T INF=numeric_limits<T>::max();
	int n=G.size();
	vector<T> d(n,INF); d[s]=0;
	priority_queue<pair<T,int>> Q; Q.emplace(0,s);
	while(!Q.empty()){
		T d0=-Q.top().first;
		int u=Q.top().second; Q.pop();
		if(d0>d[u]) continue;
		for(const auto& e:G[u]){
			int v=e.to;
			if(d[v]>d[u]+e.wt) d[v]=d[u]+e.wt, Q.emplace(-d[v],v);
		}
	}
	return d;
}

int main(){
	int n,hp,ox,oy,a[200][200];
	scanf("%d%d%d%d",&n,&hp,&ox,&oy); ox--; oy--;
	rep(i,n) rep(j,n) scanf("%d",&a[i][j]);

	weighted_graph<int> G(n*n);
	rep(i,n) rep(j,n) {
		if(i<n-1){
			G[i*n+j].emplace_back((i+1)*n+j,a[i+1][j]);
			G[(i+1)*n+j].emplace_back(i*n+j,a[i][j]);
		}
		if(j<n-1){
			G[i*n+j].emplace_back(i*n+(j+1),a[i][j+1]);
			G[i*n+(j+1)].emplace_back(i*n+j,a[i][j]);
		}
	}

	auto d=Dijkstra(G,0);
	if(hp>d[n*n-1]) return puts("YES"),0;

	if(ox==-1) return puts("NO"),0;

	auto d2=Dijkstra(G,oy*n+ox);

	puts(hp>d[oy*n+ox]&&2*(hp-d[oy*n+ox])>d2[n*n-1]?"YES":"NO");

	return 0;
}
0