結果

問題 No.90 品物の並び替え
ユーザー tshigitshigi
提出日時 2016-08-01 17:25:09
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 831 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-04-24 13:30:33
合計ジャッジ時間 1,513 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
12,160 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

inputs = STDIN.readlines.map(&:chomp)
N, M = inputs[0].split(/\s+/).map(&:to_i)
SCORES = inputs[1..-1].map { |line| line.split(/\s+/).map(&:to_i) }

class Calc
  def initialize
    @memos = Array.new(N) { Hash.new }
    @merchandises = (0...N).to_a
    @score_table = Hash.new { |h, k| h[k] = Hash.new }
    SCORES.each do |t|
      @score_table[t[0]][t[1]] = t[2]
    end
  end

  def run
    dp(0, nil, @merchandises)
  end

  def dp(score, last, rest)
    if rest.empty?
      res = 0
    elsif last && @memos[last][rest]
      res = @memos[last][rest]
    elsif last
      res = rest.map { |m|
        dp(@score_table[last][m].to_i, m, rest - [m])
      }.max
    else
      res = rest.map { |m|
        dp(0, m, rest - [m])
      }.max
    end
    @memos[last][rest] = res if last
    score + res
  end
end

puts Calc.new.run
0