結果

問題 No.20 砂漠のオアシス
ユーザー te-shte-sh
提出日時 2017-01-16 16:17:39
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,849 bytes
コンパイル時間 866 ms
コンパイル使用メモリ 111,872 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2023-09-03 00:37:53
合計ジャッジ時間 2,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.math;      // math functions

void main()
{
  auto rd = readln.split;
  auto n = rd[0].to!size_t, v = rd[1].to!int;
  auto o = point(rd[2].to!int - 1, rd[3].to!int - 1);
  auto lij = n.iota.map!(_ => readln.split.to!(int[])).array;

  auto gij = new Edge!int[][](n ^^ 2);
  foreach (j; n.iota)
    foreach (i; n.iota) {
      auto p = point(i.to!int, j.to!int);
      foreach (sib; sibPoints) {
        auto np = p + sib;
        if (np.x >= 0 && np.y >= 0 && np.x < n && np.y < n)
          gij[i + j * n] ~= Edge!int(np.x + np.y * n, lij[np.y][np.x]);
      }
    }

  writeln(calc(gij, o, v) ? "YES" : "NO");
}

auto calc(Edge!int[][] gij, point o, int v)
{
  auto n = gij.length;

  auto d1 = gij.dijkstra2(0, -1);
  if (v >= d1[n - 1]) return true;

  if (o.x < 0 || o.y < 0) return false;

  auto oi = o.x + o.y * n.to!real.sqrt.to!int;

  if (v < d1[oi]) return false;
  v = (v - d1[oi]) * 2;

  auto d2 = gij.dijkstra2(oi, -1);
  return d2[n - 1] >= v;
}

struct Point(T) {
  T x, y;

  point opBinary(string op)(point rhs) {
    static if (op == "+") return point(x + rhs.x, y + rhs.y);
  }
}

alias Point!int point;

const auto sibPoints = [point(-1, 0), point(0, -1), point(1, 0), point(0, 1)];

struct Edge(T) {
  size_t v;
  T w;
}

T[] dijkstra2(T)(Edge!T[][] ai, size_t s, T inf = 0) {
  auto n = ai.length;
  auto di = new T[](n);
  di[] = inf;

  auto qi = heapify!("a.w > b.w")(Array!(Edge!T)());

  void addNext(Edge!T e) {
    auto v = e.v, w = e.w;
    di[v] = w;
    foreach (a; ai[v])
      if (di[a.v] == inf)
        qi.insert(Edge!T(a.v, w + a.w));
  }

  addNext(Edge!T(s, 0));
  while (!qi.empty) {
    auto e = qi.front; qi.removeFront;
    if (di[e.v] == inf) addNext(e);
  }

  return di;
}
0