結果

問題 No.160 最短経路のうち辞書順最小
ユーザー te-shte-sh
提出日時 2016-09-14 14:36:22
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 1,950 bytes
コンパイル時間 1,148 ms
コンパイル使用メモリ 126,460 KB
実行使用メモリ 14,832 KB
最終ジャッジ日時 2023-09-02 22:08:28
合計ジャッジ時間 2,531 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 6 ms
4,372 KB
testcase_05 AC 11 ms
4,372 KB
testcase_06 AC 13 ms
4,452 KB
testcase_07 AC 5 ms
4,368 KB
testcase_08 AC 5 ms
4,368 KB
testcase_09 AC 4 ms
4,372 KB
testcase_10 AC 5 ms
4,368 KB
testcase_11 AC 5 ms
4,368 KB
testcase_12 AC 5 ms
4,368 KB
testcase_13 AC 5 ms
4,368 KB
testcase_14 AC 4 ms
4,368 KB
testcase_15 AC 4 ms
4,368 KB
testcase_16 AC 4 ms
4,372 KB
testcase_17 AC 5 ms
4,368 KB
testcase_18 AC 5 ms
4,372 KB
testcase_19 AC 5 ms
4,368 KB
testcase_20 AC 5 ms
4,372 KB
testcase_21 AC 4 ms
4,372 KB
testcase_22 AC 4 ms
4,368 KB
testcase_23 AC 5 ms
4,368 KB
testcase_24 AC 6 ms
4,368 KB
testcase_25 AC 4 ms
4,368 KB
testcase_26 AC 4 ms
4,368 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 58 ms
14,832 KB
testcase_29 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, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

void main()
{
  auto rd1 = readln.split.map!(to!size_t);
  auto n = rd1[0], m = rd1[1], s = rd1[2], t = rd1[3];

  auto g = new Graph!int(n);
  foreach (_; 0..m) {
    auto rd2 = readln.split;
    auto a = rd2[0].to!size_t, b = rd2[1].to!size_t, c = rd2[2].to!int;
    g[a, b] = g[b, a] = c;
  }

  writeln(g.dijkstra(s, t).map!(to!string).join(" "));
}

class Mat2(T) {
  size_t n; // size
  T[][] v; // values

  this(size_t n) {
    this.n = n;
    v = new T[][](n, n);
  }

  T opIndex(size_t i, size_t j) { return v[i][j]; }
  T opIndexAssign(T w, size_t i, size_t j) { v[i][j] = w; return w; }
}

class Mat2HasInf(T) : Mat2!T {
  T inf; // infinite

  this(size_t n, T inf = -1) {
    super(n);
    this.inf = inf;
  }

  bool isInf(size_t i, size_t j) { return v[i][j] == inf; }
}

class Graph(T) : Mat2HasInf!T {
  this(size_t n, T inf = -1) {
    super(n, inf);

    foreach (i; 0..n) v[i][] = inf;
    foreach (i; 0..n) v[i][i] = 0;
  }

  size_t[] dijkstra(size_t s, size_t g)
  {
    struct qitem {
      size_t v;
      T w;
      size_t[] r;

      int opCmp(qitem rhs) {
        if (w == rhs.w)
          return r == rhs.r ? 0 : (r < rhs.r ? -1 : 1);
        else
          return w < rhs.w ? -1 : 1;
      }
    }

    auto qi = heapify!("a > b")(Array!qitem());
    auto visited = new bool[](n);

    void addNext(qitem q) {
      auto v = q.v, w = q.w, r = q.r;
      visited[v] = true;
      foreach (i; 0..n)
        if (!isInf(v, i) && !visited[i])
          qi.insert(qitem(i, w + this[v, i], r ~ i));
    }

    addNext(qitem(s, 0, [s]));
    while (!qi.empty) {
      auto q = qi.front;
      qi.removeFront;
      if (visited[q.v]) continue;
      if (q.v == g) return q.r;
      addNext(q);
    }

    return [];
  }
}
0