結果

問題 No.527 ナップサック容量問題
ユーザー KiYugadgeterKiYugadgeter
提出日時 2019-04-08 00:46:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,061 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 65,616 KB
実行使用メモリ 41,772 KB
最終ジャッジ日時 2023-09-10 18:38:21
合計ジャッジ時間 2,386 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,376 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

int n;
int v;
int max_w = 100000;
int min_cap = -1;
int main() {
    int k = 0;
    int x = 0;
    std::cin >> n;
    int dp[n+1][100001];
    int values[n];
    int weights[n];
    for (int i = 0; i < n; i++) {
        std::cin >> values[i] >> weights[i];
        k += values[i];
        x += weights[i];
    }
    std::cin >> v;
    if (v >= k) {
        std::cout << x << std::endl;
        std::cout << "inf" << std::endl;
        return 0;
    }
    
    for (int i = 0; i <= x; i++) {
        dp[0][i] = 0;
    }
    for (int i = 1; i <= n; i++) {
        for (int p = 0; p <= x; p++) {
            if (p >= weights[i-1]) {
                dp[i][p] = std::max(dp[i-1][p-weights[i-1]] + values[i-1], dp[i-1][p]);
                if (dp[i][p] >= v) {
                    if (min_cap == -1) {
                        min_cap = weights[i-1];
                    }
                }
            }
            else {
                dp[i][p] = dp[i-1][p];
            }
        }
    }
    std::cout << dp[n][x] << " " << v << std::endl;
}
0