結果

問題 No.417 チューリップバブル
ユーザー te-sh
提出日時 2017-10-26 15:59:45
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 112,184 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 22:08:08
合計ジャッジ時間 2,590 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

void main()
{
  auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1]/2;
  auto u = new int[](n);
  foreach (i; 0..n) u[i] = readln.chomp.to!int;

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

  auto parent = new size_t[](n);
  parent[0] = n;
  auto q = DList!size_t(0), s = SList!size_t();
  while (!q.empty) {
    auto a = q.front; q.removeFront();
    s.insertFront(a);
    foreach (ei; e[a])
      if (ei.b != parent[a]) {
        parent[ei.b] = a;
        q.insertBack(ei.b);
      }
  }

  auto dp = new int[][](n, m+1);
  while (!s.empty) {
    auto a = s.front; s.removeFront();
    dp[a][0] = u[a];
    foreach (ei; e[a])
      if (ei.b != parent[a])
        foreach_reverse (i; 0..m+1)
          if (dp[a][i])
            foreach_reverse (j; 0..m+1)
              if (dp[ei.b][j] && i+j+ei.c <= m)
                dp[a][i+j+ei.c] = max(dp[a][i+j+ei.c], dp[a][i] + dp[ei.b][j]);
  }

  writeln(dp[0].maxElement);
}
0