結果

問題 No.527 ナップサック容量問題
ユーザー stkz_uqstkz_uq
提出日時 2017-06-10 00:37:44
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,174 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 55,272 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 04:16:49
合計ジャッジ時間 4,369 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>

using namespace std;

int num, item[100][2], v, w;
int W = 0, V = 0;

int func1(int n) {
    if(V == v) {
        return W;
    }
    else if(V > v || n == num) return 2147483647;

    V += item[n][0];
    W += item[n][1];
    int res1 = func1(n+1);
    V -= item[n][0];
    W -= item[n][1];
    int res2 = func1(n+1);

    return (res1 < res2) ? res1 : res2;
}

int func2(int n) {
    if(W > w)   return -1;
    if(n == num) {
        return V;
    }

    V += item[n][0];
    W += item[n][1];
    int res1 = func2(n+1);
    V -= item[n][0];
    W -= item[n][1];
    int res2 = func2(n+1);

    return (res1 > res2) ? res1 : res2;
}

int main() {
    int all = 0;

    cin >> num;
    for (int i=0; i<num; i++) {
        cin >> item[i][0] >> item[i][1];
        all += item[i][1];
    }
    cin >> v;

    int mymin = func1(0);

    w = mymin+1;
    while(func2(0) == v) {
        //cout << func2(0) << " " << w << endl;
        w++;
        
        if(w > all) {
            cout << mymin << endl << "inf" << endl;
            return 0;
        }
    }
    w--;

    if(mymin == 0)  mymin = 1;
    cout << mymin << endl << w << endl;

    return 0;
}
0