結果

問題 No.20 砂漠のオアシス
ユーザー tnakao0123tnakao0123
提出日時 2016-03-03 01:43:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,901 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 89,324 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 06:49:48
合計ジャッジ時間 1,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 20.cc: No.20 砂漠のオアシス - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200;
const int INF = 1 << 30;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

struct Stat {
  int d, x, y;
  Stat() {}
  Stat(int _d, int _x, int _y): d(_d), x(_x), y(_y) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int ls[MAX_N][MAX_N], dists[MAX_N][MAX_N];

/* subroutines */

void dijkstra(int n, int sx, int sy, int dists[MAX_N][MAX_N]) {
  for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++) dists[y][x] = INF;
  dists[sy][sx] = 0;

  priority_queue<Stat> q;
  q.push(Stat(0, sx, sy));

  while (! q.empty()) {
    Stat u = q.top(); q.pop();
    if (dists[u.y][u.x] != u.d) continue;

    for (int di = 0; di < 4; di++) {
      int vx = u.x + dxs[di], vy = u.y + dys[di];
      if (vx >= 0 && vx < n && vy >= 0 && vy < n) {
	int vd = u.d + ls[vy][vx];
	if (dists[vy][vx] > vd) {
	  dists[vy][vx] = vd;
	  q.push(Stat(vd, vx, vy));
	}
      }
    }
  }
}

/* main */

int main() {
  int n, v, ox, oy;
  cin >> n >> v >> ox >> oy;
  ox--, oy--;

  for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++) cin >> ls[y][x];

  dijkstra(n, 0, 0, dists);
  if (v > dists[n - 1][n - 1]) {
    puts("YES");
    return 0;
  }

  if (ox >= 0 && oy >= 0) {
    int d0 = dists[oy][ox];
    if (v > d0) {
      int v0 = (v - d0) * 2;
      dijkstra(n, ox, oy, dists);
      if (v0 > dists[n - 1][n - 1]) {
	puts("YES");
	return 0;
      }
    }
  }
  
  puts("NO");
  return 0;
}
0