結果

問題 No.2364 Knapsack Problem
ユーザー atcoder8atcoder8
提出日時 2023-06-30 22:49:22
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,525 bytes
コンパイル時間 1,214 ms
コンパイル使用メモリ 148,344 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-21 17:02:56
合計ジャッジ時間 6,372 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 37 ms
4,376 KB
testcase_05 AC 277 ms
4,376 KB
testcase_06 AC 37 ms
4,380 KB
testcase_07 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let input = Input::read();
    let mut used_products = vec![false; input.product_num];
    let mut used_magics = vec![false; input.magic_num];

    let ans = rec(&input, &mut used_products, &mut used_magics, 0, 0);
    println!("{}", ans);
}

fn rec(
    input: &Input,
    used_products: &mut Vec<bool>,
    used_magics: &mut Vec<bool>,
    w: i64,
    v: i64,
) -> i64 {
    if w < 0 || w > input.weight_limit {
        return 0;
    }

    if used_products.iter().all(|&x| x) && used_magics.iter().all(|&x| x) {
        return v;
    }

    let mut max_value = 0;

    for product in 0..input.product_num {
        if used_products[product] {
            continue;
        }

        used_products[product] = true;

        let value1 = rec(input, used_products, used_magics, w, v);
        let value2 = rec(
            input,
            used_products,
            used_magics,
            w + input.weights[product],
            v + input.values[product],
        );
        max_value = max_value.max(value1).max(value2);

        used_products[product] = false;
    }

    for magic in 0..input.magic_num {
        if used_magics[magic] {
            continue;
        }

        used_magics[magic] = true;

        let value1 = rec(input, used_products, used_magics, w, v);
        let value2 = rec(
            input,
            used_products,
            used_magics,
            w - input.reduce_weight[magic],
            v - input.reduce_value[magic],
        );
        max_value = max_value.max(value1).max(value2);

        used_magics[magic] = false;
    }

    max_value
}

struct Input {
    product_num: usize,
    magic_num: usize,
    weights: Vec<i64>,
    values: Vec<i64>,
    weight_limit: i64,
    reduce_weight: Vec<i64>,
    reduce_value: Vec<i64>,
}

impl Input {
    fn read() -> Self {
        let (product_num, magic_num, weight_limit) = {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                iter.next().unwrap().parse::<usize>().unwrap(),
                iter.next().unwrap().parse::<usize>().unwrap(),
                iter.next().unwrap().parse::<i64>().unwrap(),
            )
        };
        let weights: Vec<_> = {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse::<i64>().unwrap())
                .collect()
        };
        let values: Vec<_> = {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse::<i64>().unwrap())
                .collect()
        };
        let reduce_weight: Vec<_> = {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse::<i64>().unwrap())
                .collect()
        };
        let reduce_value: Vec<_> = {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse::<i64>().unwrap())
                .collect()
        };

        Self {
            product_num,
            magic_num,
            weights,
            values,
            weight_limit,
            reduce_weight,
            reduce_value,
        }
    }
}
0