結果

問題 No.34 砂漠の行商人
ユーザー te-shte-sh
提出日時 2016-09-02 11:48:03
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 2,429 bytes
コンパイル時間 953 ms
コンパイル使用メモリ 122,984 KB
実行使用メモリ 6,060 KB
最終ジャッジ日時 2023-09-02 21:52:35
合計ジャッジ時間 2,521 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 5 ms
4,368 KB
testcase_05 AC 5 ms
4,368 KB
testcase_06 AC 3 ms
4,368 KB
testcase_07 AC 8 ms
4,368 KB
testcase_08 AC 10 ms
4,372 KB
testcase_09 AC 8 ms
4,372 KB
testcase_10 AC 9 ms
4,372 KB
testcase_11 AC 8 ms
4,368 KB
testcase_12 AC 3 ms
4,372 KB
testcase_13 AC 18 ms
6,060 KB
testcase_14 AC 18 ms
4,368 KB
testcase_15 AC 3 ms
4,372 KB
testcase_16 AC 4 ms
4,372 KB
testcase_17 AC 5 ms
4,368 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 11 ms
4,372 KB
testcase_20 AC 13 ms
4,372 KB
testcase_21 AC 7 ms
4,368 KB
testcase_22 AC 7 ms
4,372 KB
testcase_23 AC 3 ms
4,368 KB
testcase_24 AC 15 ms
4,368 KB
testcase_25 AC 5 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);
  }
}

struct pointLen {
  point p;
  int len;
}

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

  auto mij = dijkstra(n, lij, g);

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

  auto cij = new int[][](n, n);
  cij.each!(a => a[] = -1);
  cij[s.y][s.x] = 0;

  auto minLen = -1;

  auto qi = DList!pointLen(pointLen(s, 0));
  while (!qi.empty) {
    auto q = qi.front, p = q.p, l = q.len;
    qi.removeFront;

    if (p == g) {
      minLen = l;
      break;
    }

    foreach (sib; sibPoints) {
      auto np = p + sib;
      if (!valid(np)) continue;

      auto nw = cij[p.y][p.x] + lij[np.y][np.x];
      if (nw >= v) continue;

      if (cij[np.y][np.x] < 0 || cij[np.y][np.x] > nw) {
        cij[np.y][np.x] = nw;
        qi.insertBack(pointLen(np, l + 1));
      }
    }
  }

  writeln(minLen);
}

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

  auto qi = 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)) continue;

      auto nw = w + lij[np.y][np.x];
      if (r[np.y][np.x] < 0 || nw < r[np.y][np.x]) {
        r[np.y][np.x] = nw;
        qi.insert(pointWeight(np, nw));
      }
    }
  }

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

  return r;
}
0