結果

問題 No.527 ナップサック容量問題
ユーザー KiYugadgeterKiYugadgeter
提出日時 2019-04-08 23:03:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,443 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 65,856 KB
実行使用メモリ 78,592 KB
最終ジャッジ日時 2023-09-14 15:45:08
合計ジャッジ時間 2,309 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,424 KB
testcase_01 AC 2 ms
5,424 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
5,460 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 19 ms
55,576 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 24 ms
70,068 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 8 ms
26,352 KB
testcase_15 AC 12 ms
41,164 KB
testcase_16 AC 2 ms
5,448 KB
testcase_17 AC 9 ms
34,616 KB
testcase_18 AC 11 ms
38,960 KB
testcase_19 AC 2 ms
5,572 KB
testcase_20 AC 8 ms
28,364 KB
testcase_21 AC 27 ms
78,592 KB
testcase_22 AC 17 ms
53,720 KB
testcase_23 AC 25 ms
77,160 KB
testcase_24 AC 5 ms
17,916 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 14 ms
48,676 KB
testcase_28 WA -
testcase_29 AC 28 ms
78,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 8 ms
36,136 KB
testcase_32 AC 10 ms
42,348 KB
testcase_33 AC 8 ms
36,268 KB
testcase_34 AC 10 ms
44,512 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:53:33: 警告: ‘num_payload’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |     for (ull r = num_payload; r <= n; r++) {
      |                               ~~^~~~
main.cpp:11:9: 備考: ‘num_payload’ はここで定義されています
   11 |     ull num_payload;
      |         ^~~~~~~~~~~

ソースコード

diff #

#include <iostream>

using ull = unsigned long long;

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