結果

問題 No.572 妖精の演奏
ユーザー siman
提出日時 2022-11-24 19:13:13
言語 Ruby
(3.4.1)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2024-10-01 08:38:20
合計ジャッジ時間 4,001 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
M = gets.to_i
A = M.times.map { gets.split.map(&:to_i) }
step_cost = Array.new(34) { Array.new(M) { Array.new(M, 0) } }

M.times do |i|
  M.times do |j|
    step_cost[0][i][j] = A[i][j]
  end
end

33.times do |i|
  M.times do |from|
    M.times do |to|
      M.times do |mid|
        ncost = step_cost[i][from][mid] + step_cost[i][mid][to]

        if step_cost[i + 1][from][to] < ncost
          step_cost[i + 1][from][to] = ncost
        end
      end
    end
  end
end

step_cnt = N - 1
ans = 0
dp = Array.new(34) { Array.new(M) { Array.new(M, 0) } }

33.times do |i|
  if step_cnt[i] == 1
    M.times do |from|
      M.times do |to|
        M.times do |mid|
          cost = dp[i][from][mid] + step_cost[i][mid][to]

          if dp[i + 1][from][to] < cost
            dp[i + 1][from][to] = cost
          end

          ans = cost if ans < cost
        end
      end
    end
  else
    M.times do |from|
      M.times do |to|
        dp[i + 1][from][to] = dp[i][from][to]
      end
    end
  end
end

puts ans
0