結果

問題 No.572 妖精の演奏
ユーザー simansiman
提出日時 2022-11-24 19:13:13
言語 Ruby
(3.3.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 16,896 KB
最終ジャッジ日時 2024-04-08 16:24:44
合計ジャッジ時間 4,417 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
16,384 KB
testcase_01 AC 107 ms
16,384 KB
testcase_02 AC 100 ms
16,384 KB
testcase_03 AC 99 ms
16,384 KB
testcase_04 AC 95 ms
16,384 KB
testcase_05 AC 118 ms
16,512 KB
testcase_06 AC 126 ms
16,512 KB
testcase_07 AC 124 ms
16,512 KB
testcase_08 AC 124 ms
16,512 KB
testcase_09 AC 141 ms
16,640 KB
testcase_10 AC 277 ms
16,896 KB
testcase_11 AC 282 ms
16,896 KB
testcase_12 AC 285 ms
16,896 KB
testcase_13 AC 308 ms
16,896 KB
testcase_14 AC 249 ms
16,896 KB
testcase_15 AC 255 ms
16,896 KB
testcase_16 AC 246 ms
16,896 KB
testcase_17 AC 260 ms
16,896 KB
testcase_18 AC 108 ms
16,512 KB
testcase_19 AC 88 ms
16,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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