Hdu2660 Accepted neck.pdf (DFS)

Source: Internet
Author: User

Problem Description
I have N precious stones, and plan to use K of them to make a neck.pdf 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.

 

Input
The 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.

 

Output
For each case, output the highest possible value of the neck.pdf.


Sample Input
1
2 1
1 1
1 1
3

Sample Output
1
Obtain the sum of the maximum values of K gems, but the weight cannot exceed W;


# Include <stdio. h> struct ston {int sa, sw ;}; struct ston s [25], tem; int su, N, K, W; void DFS (int I, int suma, int w, int k) {int j; if (su <suma) // compare the total value su = suma; if (k = K) // The number of gems cannot exceed K return; for (j = I + 1; j <= N; j ++) if (s [j]. sw + w <= W & k + 1 <= K) DFS (j, s [j]. sa + suma, s [j]. sw + w, k + 1);} int main () {int t, I, j, e, sum; scanf ("% d", & t ); while (t --) {scanf ("% d", & N, & K); for (I = 1; I <= N; I ++) scanf ("% d", & s [I]. sa, & s [I]. sw); scanf ("% d", & W); for (I = 1; I <= N; I ++) // first sort by value from large to small {e = I; for (j = I + 1; j <= N; j ++) if (s [e]. sa <s [j]. sa) e = j; tem = s [I]; s [I] = s [e]; s [e] = tem;} sum = 0; for (I = 1; I <= N; I ++) // you can view the largest if (s [I] with the total value starting with that. sw <= W & K> 0) {su = s [I]. sa; DFS (I, s [I]. sa, s [I]. sw, 1); if (su> sum) sum = su;} printf ("% d \ n", sum) ;}# include <stdio. h> struct ston {int sa, sw ;}; struct ston s [25], tem; int su, N, K, W; void DFS (int I, int suma, int w, int k) {int j; if (su <suma) // compare the total value su = suma; if (k = K) // The number of gems cannot exceed K return; for (j = I + 1; j <= N; j ++) if (s [j]. sw + w <= W & k + 1 <= K) DFS (j, s [j]. sa + suma, s [j]. sw + w, k + 1);} int main () {int t, I, j, e, sum; scanf ("% d", & t ); while (t --) {scanf ("% d", & N, & K); for (I = 1; I <= N; I ++) scanf ("% d", & s [I]. sa, & s [I]. sw); scanf ("% d", & W); for (I = 1; I <= N; I ++) // first sort by value from large to small {e = I; for (j = I + 1; j <= N; j ++) if (s [e]. sa <s [j]. sa) e = j; tem = s [I]; s [I] = s [e]; s [e] = tem;} sum = 0; for (I = 1; I <= N; I ++) // you can view the largest if (s [I] with the total value starting with that. sw <= W & K> 0) {su = s [I]. sa; DFS (I, s [I]. sa, s [I]. sw, 1); if (su> sum) sum = su;} printf ("% d \ n", sum );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.