結果

問題 No.20 砂漠のオアシス
ユーザー cielciel
提出日時 2015-12-18 11:37:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,856 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 75,212 KB
実行使用メモリ 6,576 KB
最終ジャッジ日時 2023-08-03 07:53:58
合計ジャッジ時間 1,898 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <unordered_map>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;
#define INF 99999999

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void shortestPath(const Graph &g, int s,
    vector<Weight> &dist, vector<int> &prev) {
  int n = g.size();
  dist.assign(n, INF); dist[s] = 0;
  prev.assign(n, -1);
  priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
    Edge e = Q.top(); Q.pop();
    if (prev[e.dst] != -1) continue;
    prev[e.dst] = e.src;
    for(auto &f:g[e.dst]) {
      if (dist[f.dst] > e.weight+f.weight) {
        dist[f.dst] = e.weight+f.weight;
        Q.push(Edge(f.src, f.dst, e.weight+f.weight));
      }
    }
  }
}

int main(){
	int N,V,X,Y;
	scanf("%d%d%d%d",&N,&V,&X,&Y);X--,Y--;
	vector<vector<int> >v(N);
	for(int i=0;i<N;i++){
		v[i].resize(N);
		for(int j=0;j<N;j++)scanf("%d",&v[i][j]);
	}

	///
	int H=N,W=N;
	Graph g(H*W);
	for(int i=0;i<H;i++)for(int j=0;j<W;j++){
		int x=i*W+j,y,f;
		if(i<H-1){
			y=(i+1)*W+j;
			f=v[i][j]-v[i+1][j];
			g[x].push_back(Edge(x,y,v[i+1][j])),g[y].push_back(Edge(y,x,v[i][j]));
		}
		if(j<W-1){
			y=i*W+j+1;
			f=v[i][j]-v[i][j+1];
			g[x].push_back(Edge(x,y,v[i][j+1])),g[y].push_back(Edge(y,x,v[i][j]));
		}
	}
	///

	vector<Weight>dist;
	vector<int>prev;
	shortestPath(g,0,dist,prev);
	if(dist[N*N-1]>=V){
		int o;
		if(X<0||Y<0||(o=dist[Y*W+X])>=V||(shortestPath(g,Y*W+X,dist,prev),dist[N*N-1]>=2*(V-o))){
			puts("NO");
			return 0;
		}
	}
	puts("YES");
}
0