結果

問題 No.527 ナップサック容量問題
ユーザー KiYugadgeterKiYugadgeter
提出日時 2019-04-08 21:57:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,321 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 64,400 KB
実行使用メモリ 42,444 KB
最終ジャッジ日時 2023-09-14 15:44:21
合計ジャッジ時間 2,651 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 11 ms
28,864 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 14 ms
35,640 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 5 ms
14,032 KB
testcase_15 AC 8 ms
22,412 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 6 ms
18,284 KB
testcase_18 AC 7 ms
20,356 KB
testcase_19 WA -
testcase_20 AC 5 ms
14,100 KB
testcase_21 AC 17 ms
42,444 KB
testcase_22 AC 11 ms
28,568 KB
testcase_23 AC 15 ms
39,888 KB
testcase_24 AC 3 ms
9,664 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 9 ms
26,052 KB
testcase_28 WA -
testcase_29 AC 17 ms
41,320 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 5 ms
18,028 KB
testcase_32 AC 5 ms
21,800 KB
testcase_33 AC 5 ms
19,840 KB
testcase_34 AC 6 ms
21,880 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:8:9: 警告: ‘num_payload’ may be used uninitialized [-Wmaybe-uninitialized]
    8 |     int num_payload;
      |         ^~~~~~~~~~~

ソースコード

diff #

#include <iostream>

int n;
int v;
int max_w = 100000;
int min_cap = -1;
int main() {
    int num_payload;
    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 = p;
                        num_payload = i;
                    }
                }
            }
            else {
                dp[i][p] = dp[i-1][p];
            }
        }
    }
    for (int r = num_payload; r <= n; r++) {
        for (int i = min_cap; i <= x; i++) {
            if (dp[num_payload][i] > v) {
                std::cout << min_cap << std::endl << i - 1 << std::endl;
                return 0;
            }
        }
    }
}
0