|
| 1 | +#[derive(Clone, PartialEq)] |
| 2 | +enum Select { |
| 3 | + UnSelect, |
| 4 | + Select, |
| 5 | +} |
| 6 | +struct Object { |
| 7 | + value: usize, |
| 8 | + weight: usize, |
| 9 | +} |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let input_info: Vec<usize> = input::read_numline(); |
| 13 | + let object_num = input_info[0] + 1; |
| 14 | + let max_weight = input_info[1]; |
| 15 | + let mut object_info: Vec<Object> = vec![]; |
| 16 | + let mut max_value: Vec<Vec<usize>> = vec![vec![0; max_weight + 1]; object_num]; |
| 17 | + let mut select_object: Vec<Vec<Select>> = vec![vec![Select:: UnSelect; max_weight + 1]; object_num]; |
| 18 | + |
| 19 | + for _ in 0..input_info[0] { |
| 20 | + let input_object: Vec<usize> = input::read_numline(); |
| 21 | + let object = Object{value: input_object[0], weight: input_object[1]}; |
| 22 | + object_info.push(object); |
| 23 | + } |
| 24 | + |
| 25 | + for object_index in 1..object_num { |
| 26 | + let object = &object_info[object_index - 1]; |
| 27 | + for weight in 1..=max_weight { |
| 28 | + if weight < object.weight { |
| 29 | + max_value[object_index][weight] = max_value[object_index - 1][weight] ; |
| 30 | + continue; |
| 31 | + } |
| 32 | + if max_value[object_index - 1][weight] < max_value[object_index - 1][weight - object.weight] + object.value { |
| 33 | + max_value[object_index][weight] = max_value[object_index - 1][weight - object.weight] + object.value; |
| 34 | + select_object[object_index][weight] = Select:: Select; |
| 35 | + } else { |
| 36 | + max_value[object_index][weight] = max_value[object_index - 1][weight] ; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + let mut serachindex = input_info[0]; |
| 41 | + let mut search_weight = max_weight; |
| 42 | + |
| 43 | + println!("max value is {}", max_value[object_num -1][max_weight]); |
| 44 | + |
| 45 | + while serachindex >= 1 { |
| 46 | + if select_object[serachindex][search_weight] == Select:: Select { |
| 47 | + println!("object {} is selected", serachindex -1); |
| 48 | + search_weight -= object_info[serachindex -1].weight; |
| 49 | + } |
| 50 | + serachindex -= 1; |
| 51 | + } |
| 52 | +} |
0 commit comments