結果

問題 No.2364 Knapsack Problem
コンテスト
ユーザー siman
提出日時 2023-07-01 19:21:14
言語 C++17(clang)
(clang++ 21.1.8 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 187 ms / 3,000 ms
コード長 1,854 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,271 ms
コンパイル使用メモリ 147,968 KB
実行使用メモリ 35,840 KB
最終ジャッジ日時 2026-03-29 22:30:18
合計ジャッジ時間 3,698 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:21:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   21 |   int A[N];
      |         ^
main.cpp:21:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:19:7: note: declared here
   19 |   int N, M, W;
      |       ^
main.cpp:22:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   22 |   int B[N];
      |         ^
main.cpp:22:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:19:7: note: declared here
   19 |   int N, M, W;
      |       ^
main.cpp:23:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   23 |   int C[M];
      |         ^
main.cpp:23:9: note: read of non-const variable 'M' is not allowed in a constant expression
main.cpp:19:10: note: declared here
   19 |   int N, M, W;
      |          ^
main.cpp:24:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   24 |   int D[M];
      |         ^
main.cpp:24:9: note: read of non-const variable 'M' is not allowed in a constant expression
main.cpp:19:10: note: declared here
   19 |   int N, M, W;
      |          ^
4 warnings generated.

ソースコード

diff #
raw source code

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int dp[501][1 << 7][1 << 7];

int main() {
  int N, M, W;
  cin >> N >> M >> W;
  int A[N];
  int B[N];
  int C[M];
  int D[M];
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> B[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> C[i];
  }
  for (int i = 0; i < M; ++i) {
    cin >> D[i];
  }

  memset(dp, -1, sizeof(dp));
  int ans = 0;
  dp[0][0][0] = 0;

  for (int t = 0; t < N; ++t) {

    for (int w = 0; w <= W; ++w) {
      for (int i = 0; i < N; ++i) {
        for (int n_mask = 0; n_mask < (1 << N); ++n_mask) {
          if (n_mask >> i & 1) continue;

          for (int m_mask = 0; m_mask < (1 << M); ++m_mask) {
            if (dp[w][n_mask][m_mask] < 0) continue;

            int nw = w + A[i];
            if (W < nw) continue;
            int new_mask = n_mask | (1 << i);

            dp[nw][new_mask][m_mask] = max(dp[nw][new_mask][m_mask], dp[w][n_mask][m_mask] + B[i]);
            ans = max(ans, dp[nw][new_mask][m_mask]);
          }
        }
      }
    }

    for (int w = 0; w <= W; ++w) {
      for (int n_mask = 0; n_mask < (1 << N); ++n_mask) {
        for (int m_mask = 0; m_mask < (1 << M); ++m_mask) {
          if (dp[w][n_mask][m_mask] < 0) continue;

          for (int j = 0; j < M; ++j) {
            if (m_mask >> j & 1) continue;
            int nw = w - C[j];
            int new_mask = m_mask | (1 << j);
            if (nw < 0) continue;

            dp[nw][n_mask][new_mask] = max(dp[nw][n_mask][new_mask], dp[w][n_mask][m_mask] - D[j]);
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0