結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 12 ms
28,764 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 18 ms
35,636 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 5 ms
14,024 KB
testcase_15 AC 8 ms
22,232 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 6 ms
18,176 KB
testcase_18 AC 7 ms
20,324 KB
testcase_19 WA -
testcase_20 AC 5 ms
15,912 KB
testcase_21 AC 17 ms
41,464 KB
testcase_22 AC 11 ms
27,432 KB
testcase_23 AC 15 ms
39,644 KB
testcase_24 AC 3 ms
9,748 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 9 ms
24,916 KB
testcase_28 WA -
testcase_29 AC 17 ms
41,240 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 5 ms
17,804 KB
testcase_32 AC 5 ms
22,200 KB
testcase_33 AC 5 ms
19,800 KB
testcase_34 AC 5 ms
21,864 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:50:33: 警告: ‘num_payload’ may be used uninitialized [-Wmaybe-uninitialized]
   50 |     for (int r = num_payload; r <= n; r++) {
      |                               ~~^~~~
main.cpp:8:9: 備考: ‘num_payload’ はここで定義されています
    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];
            }
        }
    }
    if (min_cap == -1) {
        min_cap = 1;
        num_payload = 1;
    }
    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