結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー te-shte-sh
提出日時 2017-12-19 17:49:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 94,788 KB
実行使用メモリ 34,244 KB
最終ジャッジ日時 2023-09-03 17:44:35
合計ジャッジ時間 8,939 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,384 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 5 ms
4,384 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 393 ms
33,352 KB
testcase_25 AC 390 ms
33,860 KB
testcase_26 AC 389 ms
33,912 KB
testcase_27 AC 398 ms
32,636 KB
testcase_28 AC 388 ms
32,112 KB
testcase_29 AC 391 ms
32,716 KB
testcase_30 AC 394 ms
33,180 KB
testcase_31 AC 398 ms
32,384 KB
testcase_32 AC 397 ms
34,244 KB
testcase_33 AC 389 ms
33,436 KB
testcase_34 AC 122 ms
17,448 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

alias graph = Graph!int;

void main()
{
  auto rd = readln.split.to!(int[]), n = rd[0], m = rd[1];

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

  auto g = new int[][](n), fg = new Edge[][](n), rg = new Edge[][](n);
  foreach (ei; e) {
    g[ei.a] ~= ei.b;
    fg[ei.a] ~= ei;
    rg[ei.b] ~= ei;
  }

  auto t = graph.topologicalSort(g);

  auto fct = new int[](n), lct = new int[](n);
  foreach (ti; t[1..$])
    fct[ti] = rg[ti].map!(ej => fct[ej.a] + ej.c).maxElement;

  lct[n-1] = fct[n-1];
  foreach_reverse (ti; t[0..$-1])
    lct[ti] = fg[ti].map!(ej => lct[ej.b] - ej.c).minElement;

  auto y = 0;
  foreach (i; 0..n) y += fct[i] != lct[i];

  writeln(fct[n-1], " ", y, "/", n);
}

template Graph(Node)
{
  import std.container;

  Node[] topologicalSort(Node[][] g)
  {
    auto n = cast(Node)(g.length), h = new size_t[](n);

    foreach (u; 0..n)
      foreach (v; g[u])
        ++h[v];

    auto st = SList!Node();
    foreach (i; 0..n)
      if (h[i] == 0) st.insertFront(i);

    Node[] ans;
    while (!st.empty()) {
      auto u = st.front; st.removeFront();
      ans ~= u;
      foreach (v; g[u]) {
        --h[v];
        if (h[v] == 0) st.insertFront(v);
      }
    }

    return ans;
  }
}
0