結果

問題 No.20 砂漠のオアシス
ユーザー te-shte-sh
提出日時 2017-05-09 10:20:49
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 2,727 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 121,984 KB
実行使用メモリ 12,956 KB
最終ジャッジ日時 2023-09-03 13:12:37
合計ジャッジ時間 2,254 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 64 ms
12,956 KB
testcase_06 AC 79 ms
12,520 KB
testcase_07 AC 79 ms
12,520 KB
testcase_08 AC 61 ms
12,312 KB
testcase_09 AC 81 ms
12,528 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 17 ms
4,496 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 17 ms
4,832 KB
testcase_20 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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!long;
  auto o = point(rd[2].to!int - 1, rd[3].to!int - 1);
  auto lij = Grid!(int, int)(n.iota.map!(_ => readln.split.to!(int[])).array);

  auto gij = new Edge!long[][](n ^^ 2);
  foreach (p; lij.points)
    foreach (np; lij.sibPoints4(p))
      gij[p.x + p.y * n] ~= Edge!long(np.x + np.y * n, lij[np]);

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

auto calc(Edge!long[][] gij, point o, long 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;

struct Grid(T, U)
{
  import std.algorithm, std.conv, std.range, std.traits, std.typecons;

  const sibs4 = [Point!U(-1, 0), Point!U(0, -1), Point!U(1, 0), Point!U(0, 1)];

  T[][] m;
  const size_t rows, cols;

  mixin Proxy!m;

  this(size_t r, size_t c) { rows = r; cols = c; m = new T[][](rows, cols); }
  this(T[][] s) { rows = s.length; cols = s[0].length; m = s; }

  pure auto dup() const { return Grid(m.map!(r => r.dup).array); }
  pure auto opIndex(Point!U p) const { return m[p.y][p.x]; }
  pure auto opIndex(size_t y) { return m[y]; }
  pure auto opIndex(size_t y, size_t x) const { return m[y][x]; }
  static if (isAssignable!T) {
    auto opIndexAssign(T v, Point!U p) { return m[p.y][p.x] = v; }
    auto opIndexAssign(T v, size_t y, size_t x) { return m[y][x] = v; }
  }
  pure auto validPoint(Point!U p) const { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; }
  pure auto points() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; }
  pure auto sibPoints4(Point!U p) const { return sibs4.map!(s => p + s).filter!(p => validPoint(p)); }
}

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