結果

問題 No.20 砂漠のオアシス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-07-01 14:55:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,825 bytes
コンパイル時間 2,149 ms
コンパイル使用メモリ 82,804 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-03 08:23:25
合計ジャッジ時間 2,080 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <array>
#include <cassert>
#include <tuple>
#include <queue>
using namespace std;

constexpr int inf = 987654321;
                            // N  E  S  W
constexpr array<int, 4> dr = {-1, 0, 1, 0},
                        dc = { 0, 1, 0,-1};
constexpr int dr_size = dr.size();

int N, V, Oc, Or;
vector<vector<int>> G, cost;

void dijk(vector<vector<int>> &cost, int sr, int sc, int V) {
  cost.assign(N, vector<int>(N, inf));
  // cost[座標] := ダメージ量
  cost[sr][sc] = 0;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
  pq.emplace(0, sr, sc);
  while(!pq.empty()) {
    int dm, r, c; tie(dm, r, c) = pq.top(); pq.pop();
    if(dm == V) { continue; }
    for(int i=0; i<dr_size; ++i) {
      int nr = r + dr[i],
          nc = c + dc[i];
      if(!(0 <= nr && nr < N && 0 <= nc && nc < N)) { continue; }
      if(cost[nr][nc] > cost[r][c] + G[nr][nc]) {
        cost[nr][nc] = cost[r][c] + G[nr][nc];
        pq.emplace(cost[nr][nc], nr, nc);
      }
    }
  }
}

int main(void) {
  scanf("%d%d%d%d", &N, &V, &Oc, &Or);
  assert(2 <= N && N <= 200);
  assert(1 <= V && V <= 500);
  assert((1 <= Or && Or <= N && 1 <= Oc && Oc <= N) || (Or == 0 && Oc == 0));
  --Or, --Oc;
  G.assign(N, vector<int>(N, 0));
  for(int r=0; r<N; ++r) {
    for(int c=0; c<N; ++c) {
      scanf("%d", &G[r][c]);
      assert(0 <= G[r][c] && G[r][c] <= 9);
    }
  }
  dijk(cost, 0, 0, V);
  if(cost[N-1][N-1] < V) {
    puts("YES");
    return 0;
  }
  // オアシスがあってそこまで辿りつける
  if(Or != -1 && Oc != -1 && cost[Or][Oc] < V) {
    int nV = (V - cost[Or][Oc]) * 2;
    dijk(cost, Or, Oc, nV);
    if(cost[N-1][N-1] < nV) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
  return 0;
}
0