Accepted Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2474    Accepted Submission(s): 973

Problem Description
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 which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother will accept.
 
Input
The first line of input is the number of cases. 
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 necklace. 
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 necklace.
 
Sample Input
1
2 1
1 1
1 1
3
 
Sample Output
1
 
 

【题意】:宝石的数目n,制成项链所需的宝石个数k,然后再给出每个宝石的价值v与重量w,还有母亲会接受的最大重量,求出在小于等于最大重量范围内,项链的价值尽可能大。

【分析】:因为数据不大可用dfs,也可以用01背包。

【代码】:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<string>
  6. #include<vector>
  7. #include<stack>
  8. #include<bitset>
  9. #include<cstdlib>
  10. #include<cmath>
  11. #include<set>
  12. #include<list>
  13. #include<deque>
  14. #include<map>
  15. #include<queue>
  16. #define ll long long
  17. using namespace std;
  18. int maxn,n,k,wei;
  19. int vis[];
  20. struct node
  21. {
  22. int v,w;
  23. }a[];
  24. /*
  25. 给出宝石的数目n,制成项链所需的宝石个数k,然后再给出每个宝石的价值与重量,
  26. 还有母亲会接受的最大重量,求出在小于等于最大重量范围内,项链的价值尽可能大。*/
  27. void dfs(int v, int w, int x, int s)//x:枚举下标,s:遍历深度(个数)
  28. {
  29. if(w==wei||s==k)
  30. {
  31. if(maxn<v)
  32. maxn=v;
  33. return ;
  34. }
  35. for(int i=x;i<=n;i++)
  36. //这个地方下标直接从x开始,因为宝石并没有要求序列问题只是求价值和,所以不需要考虑重复情况,是个很大的剪枝
  37. //举例: 3 4 5 6 == 4 3 5 6 == 5 3 6 4虽然序列不同,但是价值和相同,这样的只需要搜一次即可也就是前面搜过的重量不再搜
  38. {
  39. if(!vis[i] && w+a[i].w <= wei)//未访问+未越界(小于等于最大重量范围
  40. {
  41. vis[i]=;
  42. dfs(v+a[i].v, w+a[i].w, i+, s+);
  43. vis[i]=;
  44. }
  45. }
  46. }
  47. int main()
  48. {
  49. int t;
  50. cin>>t;
  51. while(t--)
  52. {
  53. cin>>n>>k;
  54. for(int i=;i<n;i++)
  55. cin>>a[i].v>>a[i].w;
  56. cin>>wei;
  57. memset(vis,,sizeof(vis));
  58. maxn=-;
  59. dfs(,,,);
  60. cout<<maxn<<endl;
  61. }
  62. return ;
  63. }

DFS

  1. #include <cstdio>
  2. #include <cstring>
  3. int max(int x, int y)
  4. {
  5. return x>y?x:y;
  6. }
  7. int dp[][];
  8. int main()
  9. {
  10. int W,val[],wei[];
  11. int n, k, t, i, j, l;
  12.  
  13. while(~scanf("%d",&t))
  14. {
  15. while(t--)
  16. {
  17. memset(dp,,sizeof(dp));
  18. scanf("%d %d",&n,&k);
  19. for(i = ;i < n; i++)
  20. {
  21. scanf("%d %d",&val[i],&wei[i]);
  22. }
  23. scanf("%d",&W);
  24. for(i = ; i < n; i++)
  25. {
  26. for(l = W; l >= wei[i]; l--)
  27. {
  28. for(j = ; j <= k; j++)
  29. {
  30. dp[l][j] = max(dp[l][j],dp[l-wei[i]][j-]+val[i]);
  31. }
  32. }
  33. }
  34. printf("%d\n",dp[W][k]);
  35. }
  36. }
  37. return ;
  38. }

01背包

HDU 2660 Accepted Necklace【数值型DFS】的更多相关文章

  1. HDOJ(HDU).2660 Accepted Necklace (DFS)

    HDOJ(HDU).2660 Accepted Necklace (DFS) 点我挑战题目 题意分析 给出一些石头,这些石头都有自身的价值和重量.现在要求从这些石头中选K个石头,求出重量不超过W的这些 ...

  2. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

  3. HDU 1015 Safecracker【数值型DFS】

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  5. hdu - 2660 Accepted Necklace (二维费用的背包问题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2660 f[v][u]=max(f[v][u],f[v-1][u-w[i]]+v[i]; 注意中间一层必须逆序循环 ...

  6. HDU 1427 速算24点【数值型DFS】

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. 1789: [Ahoi2008]Necklace Y型项链

    1789: [Ahoi2008]Necklace Y型项链 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 421  Solved: 258[Submit] ...

  8. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  9. Accepted Necklace

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. ARC074 E RGB Sequence DP

    ---题面--- 题解: 首先,有一个不太直观的状态,f[i][j][k][l]表示DP到i位,三种颜色最后出现的位置分别是j, k, l的方案数.因为知道了三种颜色最后出现的位置,因此也可以得知以当 ...

  2. BZOJ5322 [Jxoi2018]排序问题 【贪心】

    题目链接 BZOJ5322 题解 意思就是使有序的排列尽量少 就是使相同的数尽量少 然后大力贪心即可 #include<algorithm> #include<iostream> ...

  3. [BZOJ2090/2089] [Poi2010]Monotonicity 2/Monotonicity 树状数组优化dp

    这个dp乍看不科学,仔细一看更不科学,所以作为一个执着BOY,我决定要造数据卡死波兰人民,但是我造着造着就......证出来了......... 这个就是把 < > =分开讨论每次找到f[ ...

  4. CentOS系统缺少库文件解决办法

    By francis_hao    May 31,2017   程序在编译时出现缺少库文件的提示,如下: as: error while loading shared libraries: libz. ...

  5. 门户系统整合sso cookie共享及显示用户信息

    1.1 门户系统整合sso 在门户系统点击登录连接跳转到登录页面.登录成功后,跳转到门户系统的首页,在门户系统中需要从cookie中 把token取出来.所以必须在登录成功后把token写入cooki ...

  6. MAC地址的介绍(单播、广播、组播、数据收发)

    MAC地址组成 网络设备的MAC地址是全球唯一的.MAC地址长度为48比特,通常用十六进制表示.MAC地址包含两部分:前24比特是组织唯一标识符(OUI,OrganizationallyUniqueI ...

  7. 自旋锁、排队自旋锁、MCS锁、CLH锁

    转载自:http://coderbee.net/index.php/concurrent/20131115/577 自旋锁(Spin lock) 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他 ...

  8. Python基础(4)_集合、布尔类型

    一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...

  9. C++中的垃圾回收和内存管理(续)

    boost memory的gc_allocator的使用 首先编译生成boost-memory的库,由于生成的是.so的动态库,所以需要在运行程序之前,将库文件的路径添加到LD_LIBRARY_PAT ...

  10. Chocolatey 使用

    最近空了下来不干点什么就感觉脑袋热,可是出过的问题挖过的坑还是要自己去解决. 一直网络不好安装choco一直都是报错,今天又建立了chocolatey 在windows上来用,网络问题解决了,类似于m ...