题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=565

题意很好理解,普通的01背包,dp[i - 1][j]表示在前i - 1件物品中选取若干物品放入容量为j背包所得到的最大的价值,dp[i - 1][j - w[i]] + v[i]表示前i - 1件物品中选取若干物品放入容量为j - w[i]背包所得到的最大的价值加上第i件物品的价值。这里同理,但是要记录选取的物品,觉得倒着过来就可以了。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. using namespace std;
  5. const int MAXN = ;
  6. int dp[][MAXN] , v[];
  7. vector <int> G;
  8. int main()
  9. {
  10. ios::sync_with_stdio(false);
  11. int V , n;
  12. while(cin >> V >> n) {
  13. G.clear();
  14. for(int i = ; i <= n ; i++) {
  15. cin >> v[i];
  16. }
  17. memset(dp , , sizeof(dp));
  18. for(int i = ; i <= n ; i++) {
  19. for(int j = ; j <= V ; j++) {
  20. if(j < v[i])
  21. dp[i][j] = dp[i - ][j];
  22. else
  23. dp[i][j] = max(dp[i - ][j] , dp[i - ][j - v[i]] + v[i]);
  24. }
  25. }
  26. int i = n , j = V;
  27. while(i > && j > ) {
  28. if(j < v[i]) {
  29. i--;
  30. continue;
  31. }
  32. if(dp[i][j] == dp[i - ][j - v[i]] + v[i]) {
  33. j -= v[i];
  34. G.push_back(v[i]);
  35. }
  36. i--;
  37. }
  38. for(int i = G.size() - ; i > ; i--) {
  39. cout << G[i] << " ";
  40. }
  41. cout << G[] << " sum:";
  42. cout << dp[n][V] << endl;
  43. }
  44. }

UVA 624 - CD (01背包 + 打印物品)的更多相关文章

  1. uva 624 CD 01背包打印路径

    // 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...

  2. UVA 624 ---CD 01背包路径输出

    DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...

  3. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  4. UVA624 CD,01背包+打印路径,好题!

    624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...

  5. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  6. UVA 624 CD(01背包+输出方案)

    01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...

  7. UVA 624 CD(DP + 01背包)

    CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...

  8. uva 624 CD (01背包)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  9. CD(01背包)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

随机推荐

  1. IIS 10.0 无法安装 URL rewrite重写模块 2.0解决办法

    [问题描述]系统升级到Windows10后,IIS是10.0的,发现无法安装 URLRewrite重写模块 2.0. [解决办法]打开注册表编辑器,在HKEY_LOCAL_MACHINE\SOFTWA ...

  2. 手动配置gradle

    最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...

  3. crtbegin_dynamic.o: No such file: No such file or directory

    /homesec/android2/zhangbin/053work3/hi050src/HiSTBAndroidV400R001C00SPC050B012/prebuilt/linux-x86/to ...

  4. 使php支持mbstring库

    多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...

  5. zoj 2770 Burn the Linked Camp (差分约束系统)

    // 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...

  6. mkswap 把一个分区格式化成为swap交换区

    mkswap /dev/sda* //创建此分区为swap 交换分区swapon /dev/sda* //加载交换分区swapoff /dev/sda* //关闭交换分区: 若想要开机自动挂载:vi ...

  7. 【MySQL for Mac】终极解决——MySQL在Mac的字符集设置

    这个问题烦恼一天了,现在终于得以解决.分享给大家 首先贴出来,亲测不可行的博客连接: http://www.2cto.com/database/201305/215563.html http://bl ...

  8. MyBatis批量删除 多态sql,构建in语句

    <!--==========================删除==================================== -->    <delete id=&quo ...

  9. Delphi 利用TComm组件 Spcomm 实现串行通信

    Delphi 利用TComm组件 Spcomm 实现串行通信 摘要:利用Delphi开发工业控制系统软件成为越来越多的开发人员的选择,而串口通信是这个过程中必须解决的问题之一.本文在对几种常用串口通信 ...

  10. delphi7在win7系统如何安装spcomm控件

    1.先准备好串口控件SPCOMM,例如把它放在F盘的工具安装文件夹下,等一下加载时需要用到. 2.打开delphi7软件. 3.按下上面的Component>Install  Component ...