結果

問題 No.417 チューリップバブル
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2016-09-27 00:55:56
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,657 bytes
コンパイル時間 124 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 24,988 KB
最終ジャッジ日時 2024-05-01 05:37:22
合計ジャッジ時間 27,373 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
19,356 KB
testcase_01 AC 78 ms
12,288 KB
testcase_02 AC 78 ms
12,288 KB
testcase_03 AC 81 ms
12,288 KB
testcase_04 AC 74 ms
12,160 KB
testcase_05 AC 77 ms
12,160 KB
testcase_06 AC 77 ms
12,288 KB
testcase_07 AC 81 ms
12,288 KB
testcase_08 AC 82 ms
12,160 KB
testcase_09 AC 85 ms
12,288 KB
testcase_10 AC 133 ms
12,928 KB
testcase_11 AC 193 ms
13,056 KB
testcase_12 AC 189 ms
13,056 KB
testcase_13 AC 216 ms
13,184 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 178 ms
12,928 KB
testcase_17 AC 992 ms
13,952 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,747 ms
14,720 KB
testcase_23 WA -
testcase_24 AC 183 ms
12,288 KB
testcase_25 WA -
testcase_26 AC 374 ms
13,440 KB
testcase_27 AC 736 ms
14,208 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 1,872 ms
14,720 KB
testcase_31 TLE -
testcase_32 AC 81 ms
12,160 KB
testcase_33 AC 78 ms
12,288 KB
testcase_34 WA -
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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|
        if G[0][i] <= m
            dp[i][G[0][i]] = T[i] + T[0]
        end
    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