結果

問題 No.2364 Knapsack Problem
ユーザー osada-yumosada-yum
提出日時 2023-09-26 19:43:32
言語 Fortran
(gFortran 13.2.0)
結果
AC  
実行時間 31 ms / 3,000 ms
コード長 1,765 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 29,636 KB
実行使用メモリ 66,636 KB
最終ジャッジ日時 2023-09-26 19:43:35
合計ジャッジ時間 2,508 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 4 ms
7,672 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 5 ms
9,688 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 31 ms
66,568 KB
testcase_13 AC 29 ms
66,592 KB
testcase_14 AC 29 ms
66,632 KB
testcase_15 AC 30 ms
66,624 KB
testcase_16 AC 30 ms
66,536 KB
testcase_17 AC 30 ms
66,628 KB
testcase_18 AC 30 ms
66,636 KB
testcase_19 AC 30 ms
66,548 KB
testcase_20 AC 29 ms
66,568 KB
testcase_21 AC 30 ms
66,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

!> 追加する順番が大事になりそうだから, bitDPでがんばる.
program yukicoder_2364
  use, intrinsic :: iso_fortran_env
  implicit none
  integer(int32) :: n, m, max_w
  integer(int32), allocatable :: ws(:)
  integer(int64), allocatable :: vs(:)
  integer(int64), allocatable :: dp(:, :)
  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(:, :)
  call bit_dp(0, 0, dp)
  write(output_unit, '(i0)') maxval(dp(:, :))
contains
  impure recursive subroutine bit_dp(state, weight, dp)
    integer(int32), intent(in) :: state
    integer(int32), intent(in) :: weight
    integer(int64), intent(inout) :: dp(0:, 0:)
    integer(int32) :: next_state
    integer(int32) :: v, next_w
    ! if (dp(weight, state) /= -1) return
    ! if (weight == 0 .and. state == 0) &
    !      dp(0, 0) = 0_int64
    do v = 1, n + m
       if (btest(state, v - 1)) cycle
       next_w = weight + ws(v)
       if (next_w < 0 .or. next_w > max_w) cycle
       next_state = ibset(state, v - 1)
       ! write(error_unit, '(2(i0, 1x), 2(b15.15, 1x), i0)') v, weight, state, next_state, dp(next_w, next_state)
       if (dp(next_w, next_state) < dp(weight, state) + vs(v)) then
          dp(next_w, next_state) = dp(weight, state) + vs(v)
          call bit_dp(next_state, next_w, dp)
       end if
    end do
  end subroutine bit_dp
end program yukicoder_2364
0