結果

問題 No.614 壊れたキャンパス
ユーザー te-shte-sh
提出日時 2017-12-14 10:16:10
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 716 ms / 2,000 ms
コード長 2,430 bytes
コンパイル時間 1,078 ms
コンパイル使用メモリ 114,360 KB
実行使用メモリ 95,600 KB
最終ジャッジ日時 2023-09-03 17:32:58
合計ジャッジ時間 10,500 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 655 ms
87,560 KB
testcase_09 AC 635 ms
84,612 KB
testcase_10 AC 555 ms
90,824 KB
testcase_11 AC 676 ms
82,020 KB
testcase_12 AC 716 ms
81,748 KB
testcase_13 AC 701 ms
82,956 KB
testcase_14 AC 663 ms
89,944 KB
testcase_15 AC 525 ms
88,216 KB
testcase_16 AC 604 ms
85,580 KB
testcase_17 AC 406 ms
95,600 KB
testcase_18 AC 527 ms
77,092 KB
testcase_19 AC 523 ms
80,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

alias graph = Graph!(long, int, 10L^^18);
alias edge = graph.Edge;

void main()
{
  auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1], k = rd[2], s = rd[3], t = rd[4];

  if (n == 1) {
    writeln((s-t).abs);
    return;
  }

  struct Rouka { int a, b, c; }
  auto roukas = new Rouka[](m);
  foreach (i; 0..m) {
    auto rd2 = readln.splitter;
    auto a = rd2.front.to!int-1; rd2.popFront();
    auto b = rd2.front.to!int;   rd2.popFront();
    auto c = rd2.front.to!int;
    roukas[i] = Rouka(a, b, c);
  }

  auto r = new int[][](n);
  foreach (rouka; roukas) {
    r[rouka.a] ~= rouka.b;
    r[rouka.a+1] ~= rouka.c;
  }

  foreach (ri; r) ri.sort();

  auto h = new int[int][](n), cnt = 1;
  foreach (i, ri; r)
    foreach (rij; ri)
      h[i][rij] = cnt++;

  auto g = new edge[][](cnt+1);

  foreach (i, ri; r)
    if (ri.length >= 2)
      foreach (j; 0..ri.length-1) {
        auto u = h[i][ri[j]], v = h[i][ri[j+1]], w = ri[j+1]-ri[j];
        g[u] ~= edge(u, v, w);
        g[v] ~= edge(v, u, w);
      }

  foreach (rouka; roukas) {
    auto u = h[rouka.a][rouka.b], v = h[rouka.a+1][rouka.c];
    g[u] ~= edge(u, v, 0);
  }

  foreach (rij; r[0]) {
    auto v = h[0][rij];
    g[0] ~= edge(0, v, (s-rij).abs);
  }

  foreach (rij; r[$-1]) {
    auto u = h[$-1][rij];
    g[u] ~= edge(u, cnt, (t-rij).abs);
  }

  auto dist = graph.dijkstra(g, 0);

  writeln(dist[cnt] == graph.inf ? -1 : dist[cnt]);
}

template Graph(Wt, Node, Wt _inf = 10 ^^ 9, Node _sent = Node.max)
{
  import std.container;

  const inf = _inf, sent = _sent;

  struct Edge
  {
    Node src, dst;
    Wt wt;
  }

  Wt[] dijkstra(Edge[][] g, Node s)
  {
    Wt[] dist;
    Node[] prev;
    dijkstra(g, s, dist, prev);
    return dist;
  }

  void dijkstra(Edge[][] g, Node s, out Wt[] dist, out Node[] prev)
  {
    auto n = g.length;

    dist = new Wt[](n);
    dist[] = inf;
    dist[s] = 0;

    prev = new Node[](n);
    prev[] = sent;

    auto q = heapify!("a.wt > b.wt")(Array!Edge(Edge(sent, s, 0)));
    while (!q.empty) {
      auto e = q.front; q.removeFront();
      if (prev[e.dst] != sent) continue;
      prev[e.dst] = e.src;
      foreach (f; g[e.dst]) {
        auto w = e.wt + f.wt;
        if (dist[f.dst] > w) {
          dist[f.dst] = w;
          q.insert(Edge(f.src, f.dst, w));
        }
      }
    }
  }
}
0