Accepted neck.pdf
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2235 accepted submission (s): 868
Problem descriptioni have n precious stones, and plan to use K of them to make a neck1_for my mother, but she won't accept a neck1_which is too heavy. given the value and the weight of each precious stone, please help me find out the most valuable
Neckpolicmy mother will accept.
Inputthe first line of input is the number of instances.
For each case, the first line contains two integers n (n <= 20), the total number of stones, and K (k <= N ), the exact number of stones To Make A necktasks.
Then n lines follow, each containing two integers: A (a <= 1000), representing the value of each precious stone, and B (B <= 1000), its weight.
The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.
Outputfor each case, output the highest possible value of the neck.pdf.
Sample Input
1 2 1 1 1 1 1 3
Sample output
1
Sourcehdu boys' Open Session-start a holiday before a girl (from
Whu)
Recommendzty
# Include <iostream> # include <limits> using namespace STD; int C, N, K, max_w; int max_val, sum_val, sum_w; struct node {int val; int W; int visit;} A [22]; void DFS (INT start, int count) // start point of the Start cycle, Count records the number of stones used {int I; If (sum_w> max_w) // pruning return; If (count> K) return; If (k = count & sum_w <= max_w) {If (max_val <sum_val) max_val = sum_val; return ;} for (I = start; I <n; I ++) // if (a [I]. visit = 0) {sum_val + = A [I]. val; sum_w + = A [I]. w; A [I]. visit = 1; DFS (I + 1, Count + 1); sum_val-= A [I]. val; sum_w-= A [I]. w; A [I]. visit = 0 ;}} int main () {int I; CIN> C; while (c --) {CIN> N> K; for (I = 0; I <n; I ++) {CIN> A [I]. val; CIN> A [I]. w; A [I]. visit = 0;} CIN> max_w; sum_w = sum_val = max_val = 0; DFS (0, 0); cout <max_val <Endl;} return 0 ;}