結果

問題 No.2364 Knapsack Problem
ユーザー osada-yumosada-yum
提出日時 2023-09-26 19:14:50
言語 Fortran
(gFortran 13.2.0)
結果
WA  
実行時間 -
コード長 1,820 bytes
コンパイル時間 1,257 ms
コンパイル使用メモリ 29,732 KB
実行使用メモリ 9,620 KB
最終ジャッジ日時 2023-09-26 19:15:02
合計ジャッジ時間 8,826 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,424 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2,106 ms
7,660 KB
testcase_08 WA -
testcase_09 AC 344 ms
9,620 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

!> 追加する順番が大事になりそうだから, bitDPでがんばる.
program yukicoder_2364
  use, intrinsic :: iso_fortran_env
  implicit none
  integer(int32) :: n, m, max_w
  integer(int64) :: maxi
  integer(int32), allocatable :: ws(:)
  integer(int64), allocatable :: vs(:)
  integer(int64), allocatable :: dp(:, :)
  integer(int32) :: i
  read(input_unit, *) n, m, max_w
  allocate(ws(n+m), vs(n+m))
  read(input_unit, *) ws(1:n)
  read(input_unit, *) vs(1:n)
  read(input_unit, *) ws(n+1:n+m)
  read(input_unit, *) vs(n+1:n+m)
  ws(n+1:n+m) = - ws(n+1:n+m)
  vs(n+1:n+m) = - vs(n+1:n+m)
  ! write(error_unit, '(*(i0, 1x))') ws(:)
  ! write(error_unit, '(*(i0, 1x))') vs(:)
  allocate(dp(0:max_w, 0:ishft(1_int32, n + m)-1), source = -1_int64)
  dp(0, 0) = 0_int64
  ! write(error_unit, '(*(i0, 1x))') dp(:, :)
  maxi = 0_int64

  do i = 0, max_w
     call bit_dp(ishft(1_int32, n + m) - 1, i, maxi, dp)
  end do
  write(output_unit, '(i0)') maxi
contains
  pure recursive subroutine bit_dp(state, weight, maxi, dp)
    integer(int32), intent(in) :: state
    integer(int32), intent(in) :: weight
    integer(int64), intent(inout) :: maxi, dp(0:, 0:)
    integer(int32) :: next_state
    integer(int32) :: v, bef_w
    if (dp(weight, state) /= -1) return
    do v = 1, n + m
       if (.not. btest(state, v - 1)) cycle
       bef_w = weight - ws(v)
       if (bef_w < 0 .or. bef_w > max_w) cycle
       next_state = ieor(state, ishft(1_int32, v - 1))
       call bit_dp(next_state, bef_w, maxi, dp)
       if (dp(bef_w, next_state) == -1) cycle
       dp(weight, state) = max(dp(weight, state), dp(bef_w, next_state) + vs(v))
       ! write(output_unit, '(b15.15, 1x, i0)') state, dp(bef_w, next_state)
    end do
    maxi = max(maxi, dp(weight, state))
  end subroutine bit_dp
end program yukicoder_2364
0