Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=2660
Accepted necklacedescription
I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won ' t accept a necklace whic H is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother would a Ccept.
Input
The first line of input is the number of cases.
For each case, the first line contains integers n (n <=), the total number of stones, and K (k <= N), the Exa CT number of stones to make a necklace.
Then N lines follow, each containing-integers:a (a<=1000), representing the value of each precious stone, and B (b <=1000), its weight.
The last line of all case contains an integer w, the maximum weight my mother would accept, W <= 1000.
Output
For each case, output the highest possible value of the necklace.
Sample Input
1
2 1
1 1
1 1
3
Sample Output
1
Dfs..
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <set>using std::max;using std::sort;using std: :p air;using std::swap;using std::queue;using std::multiset; #define PB (E) push_back (e) #define SZ (c) (int) (c). Size () # Define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) Decltype ((c). Begin ()) #define CLS (arr , Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0 ; I < (int) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 30;const int INF = 0x3f3f3f3f;typedef unsigned long long ull;struct Node {int V, W;} A[n];bool Vis[n];int W, K, N, ans;void dfs (int cur, int W, int v, int tot) {if (tot = = K) {ans = max (ans, v); return;} for (int i = cur; i < n; i++) {if (!vis[i] && tot + 1 <= K && W + a[i].w <= W) {Vis[i] = TRUE;DFs (i + 1, W + a[i].w, V + a[i].v, tot + 1); vis[i] = false;}}} int main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint t;scanf ("%d", &t); while (t--) {ans =-inf;scanf ("%d%d", &n, &k); Rep (i, N) {vis[i] = false;scanf ("%d%d", &A[I].V, &A[I].W); }SCANF ("%d", &w);d FS (0, 0, 0, 0);p rintf ("%d\n", ans); return 0;}
Hdu 2660 Accepted necklace