結果

問題 No.417 チューリップバブル
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2016-09-27 00:53:02
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,617 bytes
コンパイル時間 50 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-05-01 05:36:34
合計ジャッジ時間 10,319 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
12,160 KB
testcase_01 AC 76 ms
12,160 KB
testcase_02 AC 77 ms
12,160 KB
testcase_03 AC 77 ms
12,160 KB
testcase_04 AC 75 ms
12,160 KB
testcase_05 AC 74 ms
12,160 KB
testcase_06 AC 79 ms
12,288 KB
testcase_07 AC 78 ms
12,160 KB
testcase_08 RE -
testcase_09 AC 147 ms
12,288 KB
testcase_10 AC 130 ms
12,928 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 AC 75 ms
12,160 KB
testcase_33 AC 75 ms
12,160 KB
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

INF = 1 << 30
n,m = gets.split.map(&:to_i)

T = n.times.map { gets.to_i }
E = (n-1).times.map { gets.split.map(&:to_i) }
O = Array.new(n).tap do |used|
    g = Array.new(n) { [] }
    E.each do |a,b,c|
        g[a] << b
        g[b] << a
    end

    index = 0
    stack = [0]
    until stack.empty?
        v = stack.pop
        used[v] = index
        g[v].each do |to|
            if !used[to]
                stack << to
            end
        end
        index += 1
    end
end

G = Array.new(n) { Array.new(n, INF) }.tap do |d|
    n.times do |i|
        d[i][i] = 0
    end

    E.each do |a,b,c|
        d[a][b] = d[b][a] = c
    end
    
    n.times do |i|
        n.times do |j|
            n.times do |k|
                if d[i][j] > d[i][k] + d[k][j]
                    d[j][i] = d[i][j] = d[i][k] + d[k][j]
                end
            end
        end
    end
end

dp = Array.new(n) { Array.new(m + 1) }.tap do |dp|
    dp[0][0] = T[0]
    (1 ... n).each do |i|
        dp[i][G[0][i]] = T[i] + T[0]
    end
    
    (1 .. m).each do |t|
        (1 ... n).each do |i|
            if dp[i][t]
                n.times.select {|v| O[v] > O[i]}.each do |j|
                    if t + G[i][j] <= m
                        dp[j][t + G[i][j]] = [dp[j][t + G[i][j]] ||= 0, dp[i][t] + T[j] ].max
                    end
                end
                unless i == 0
                    if t + G[i][0] <= m
                        dp[0][t + G[i][0]] = [ dp[0][t + G[i][0]] ||= 0, dp[i][t] ].max
                    end
                end
            end
        end
    end
end

puts dp[0].compact.max



0