結果

問題 No.20 砂漠のオアシス
ユーザー te-shte-sh
提出日時 2016-09-01 15:26:46
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 1,968 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 119,416 KB
実行使用メモリ 4,788 KB
最終ジャッジ日時 2023-09-02 21:50:10
合計ジャッジ時間 2,970 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 RE -
testcase_04 AC 5 ms
4,372 KB
testcase_05 AC 47 ms
4,788 KB
testcase_06 AC 57 ms
4,464 KB
testcase_07 AC 57 ms
4,468 KB
testcase_08 AC 69 ms
4,424 KB
testcase_09 AC 58 ms
4,464 KB
testcase_10 AC 1 ms
4,368 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 4 ms
4,372 KB
testcase_13 AC 5 ms
4,372 KB
testcase_14 AC 7 ms
4,372 KB
testcase_15 AC 6 ms
4,368 KB
testcase_16 AC 14 ms
4,368 KB
testcase_17 AC 10 ms
4,368 KB
testcase_18 AC 12 ms
4,372 KB
testcase_19 AC 14 ms
4,372 KB
testcase_20 AC 2 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random;
import std.string, std.conv, std.stdio, std.typecons;

struct point {
  int x;
  int y;

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

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

struct pointWeight {
  point p;
  int weight;

  int opCmp(pointWeight rhs) {
    return weight == rhs.weight ? 0 : (weight < rhs.weight ? -1 : 1);
  }
}

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

  auto s = point(0, 0), e = point(n - 1, n - 1);
  auto dummy = point(-1, -1);

  auto r1 = dijkstra(n, lij, s, e, o);
  auto r2 = dijkstra(n, lij, s, o, dummy);
  auto r3 = dijkstra(n, lij, o, e, dummy);

  if (v - r1 > 0 || (v - r2) * 2 - r3 > 0)
    writeln("YES");
  else
    writeln("NO");
}

int dijkstra(int n, int[][] lij, point s, point e, point o)
{
  auto memo = new int[][](n, n);
  memo.each!((a) { a[] = -1; });
  memo[s.y][s.x] = 0;

  auto pi = heapify!("a > b")(Array!pointWeight());

  void addPointWeight(pointWeight pw) {
    bool valid(point p) {
      return p.x >= 0 && p.x < n && p.y >= 0 && p.y < n;
    }

    auto p = pw.p, w = pw.weight;

    foreach (sib; sibPoints) {
      auto np = p + sib;

      if (!valid(np) || np == o) continue;

      auto nw = w + lij[np.y][np.x];

      if (memo[np.y][np.x] < 0 || nw < memo[np.y][np.x]) {
        memo[np.y][np.x] = nw;
        pi.insert(pointWeight(np, nw));
      }
    }
  }

  addPointWeight(pointWeight(s, 0));
  while (!pi.empty) {
    auto pw = pi.front, p = pw.p, w = pw.weight;
    pi.removeFront;

    if (p == e) return w;
    addPointWeight(pw);
  }

  return 0;
}
0