結果
問題 | No.2364 Knapsack Problem |
ユーザー | atcoder8 |
提出日時 | 2023-06-30 23:22:38 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,748 bytes |
コンパイル時間 | 13,632 ms |
コンパイル使用メモリ | 383,788 KB |
実行使用メモリ | 128,040 KB |
最終ジャッジ日時 | 2024-07-07 11:16:27 |
合計ジャッジ時間 | 21,995 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 106 ms
17,712 KB |
testcase_11 | WA | - |
testcase_12 | AC | 993 ms
128,000 KB |
testcase_13 | AC | 843 ms
127,948 KB |
testcase_14 | AC | 510 ms
64,996 KB |
testcase_15 | AC | 1,328 ms
127,936 KB |
testcase_16 | AC | 861 ms
127,956 KB |
testcase_17 | AC | 686 ms
64,980 KB |
testcase_18 | AC | 882 ms
127,936 KB |
testcase_19 | AC | 1,493 ms
128,040 KB |
testcase_20 | AC | 1,379 ms
127,896 KB |
testcase_21 | AC | 798 ms
127,940 KB |
ソースコード
use std::collections::HashMap; fn main() { let input = Input::read(); let mut memo = HashMap::new(); let ans = rec(&input, 0, 0, 0, 0, &mut memo); println!("{}", ans); } fn rec( input: &Input, used_products: usize, used_magics: usize, w: i64, v: i64, memo: &mut HashMap<(usize, usize, i64, i64), i64>, ) -> i64 { if w < 0 || w > input.weight_limit { return 0; } if used_magics == (1 << input.product_num) - 1 && used_magics == (1 << input.magic_num) - 1 { return v; } if let Some(&sum_v) = memo.get(&(used_products, used_magics, w, v)) { return sum_v; } let mut max_value = 0; for product in 0..input.product_num { if (used_products >> product) & 1 == 1 { continue; } let next_used_products = used_products | (1 << product); let value1 = rec(input, next_used_products, used_magics, w, v, memo); let value2 = rec( input, next_used_products, used_magics, w + input.weights[product], v + input.values[product], memo, ); max_value = max_value.max(value1).max(value2); } for magic in 0..input.magic_num { if (used_magics >> magic) & 1 == 1 { continue; } let next_used_magics = used_magics | (1 << magic); let value1 = rec(input, used_products, next_used_magics, w, v, memo); let value2 = rec( input, used_products, next_used_magics, w - input.reduce_weight[magic], v - input.reduce_value[magic], memo, ); max_value = max_value.max(value1).max(value2); } memo.insert((used_products, used_magics, w, v), max_value); 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, } } }