結果

問題 No.20 砂漠のオアシス
ユーザー chocorusk
提出日時 2018-10-17 06:29:48
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 2,673 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 106,868 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-12 18:50:37
合計ジャッジ時間 1,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<P, int> Pint;
typedef pair<P, P> PP;
const int INF=1e9;
int main()
{
	int n, v, ox, oy;
	cin>>n>>v>>ox>>oy;
	int l[201][201];
	for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) cin>>l[j][i];
	queue<Pint> que[10];
	priority_queue<PP, vector<PP>, greater<PP>> quefr;
	que[0].push(Pint(P(1, 1), 0));
	quefr.push(PP(P(0, 0), P(1, 1)));
	int d[201][201];
	for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) d[i][j]=INF;
	d[1][1]=0;
	int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};
	while(!quefr.empty()){
		PP p=quefr.top(); quefr.pop();
		int i0=p.first.second;
		que[i0].pop();
		if(!que[i0].empty()){
			Pint p1=que[i0].front();
			quefr.push(PP(P(p1.second, i0), p1.first));
		}
		int x=p.second.first, y=p.second.second;
		if(p.first.first>d[x][y]) continue;
		for(int k=0; k<4; k++){
			if(0<x+dx[k] && x+dx[k]<=n && 0<y+dy[k] && y+dy[k]<=n){
				if(d[x][y]+l[x+dx[k]][y+dy[k]]<d[x+dx[k]][y+dy[k]]){
					d[x+dx[k]][y+dy[k]]=d[x][y]+l[x+dx[k]][y+dy[k]];
					if(que[l[x+dx[k]][y+dy[k]]].empty()){
						quefr.push(PP(P(d[x+dx[k]][y+dy[k]], l[x+dx[k]][y+dy[k]]), P(x+dx[k], y+dy[k])));
					}
					que[l[x+dx[k]][y+dy[k]]].push(Pint(P(x+dx[k], y+dy[k]), d[x+dx[k]][y+dy[k]]));
				}
			}
		}
	}
	if(v>d[n][n]){
		cout<<"YES"<<endl;
		return 0;
	}
	if(ox==0 && oy==0){
		cout<<"NO"<<endl;
		return 0;
	}
	if(v<=d[ox][oy]){
		cout<<"NO"<<endl;
		return 0;
	}
	v=(v-d[ox][oy])*2;
	que[0].push(Pint(P(ox, oy), 0));
	quefr.push(PP(P(0, 0), P(ox, oy)));
	for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) d[i][j]=INF;
	d[ox][oy]=0;
	while(!quefr.empty()){
		PP p=quefr.top(); quefr.pop();
		int i0=p.first.second;
		que[i0].pop();
		if(!que[i0].empty()){
			Pint p1=que[i0].front();
			quefr.push(PP(P(p1.second, i0), p1.first));
		}
		int x=p.second.first, y=p.second.second;
		if(p.first.first>d[x][y]) continue;
		for(int k=0; k<4; k++){
			if(0<x+dx[k] && x+dx[k]<=n && 0<y+dy[k] && y+dy[k]<=n){
				if(d[x][y]+l[x+dx[k]][y+dy[k]]<d[x+dx[k]][y+dy[k]]){
					d[x+dx[k]][y+dy[k]]=d[x][y]+l[x+dx[k]][y+dy[k]];
					if(que[l[x+dx[k]][y+dy[k]]].empty()){
						quefr.push(PP(P(d[x+dx[k]][y+dy[k]], l[x+dx[k]][y+dy[k]]), P(x+dx[k], y+dy[k])));
					}
					que[l[x+dx[k]][y+dy[k]]].push(Pint(P(x+dx[k], y+dy[k]), d[x+dx[k]][y+dy[k]]));
				}
			}
		}
	}
	if(v>d[n][n]) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}
0