I love sneakers!

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

Problem Description
After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.

There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
 
Input
Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
 
Output
For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
 
Sample Input
5 10000 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
 
Sample Output
255
 
Source
 
 
题目意思:
有n种物品,每种可能有多个,每个有不同体积和价值。初始手中有V的背包,现将所有种类至少一个装进背包中,若无法达到目标输出Impossible,否则输出最大的价值。
 
思路:
原始的分组背包问题是每种选或不选,而且选的话只能选一个。而这道题是每种必选,选的话可以选多个。
选多个的条件很容易,把分组背包for V 和 for 第i个  互换一下层数即可。
必选的条件有点难度,若必选的话,那么dp[i][j]由dp[i][j-v[k]]和dp[i-1][j-v[k]]转移即 在第i组中去掉v[k]体积和在前i-1组中去掉v[k]体积,在取max时不能用max(dp[i-1][j],dp[i-1][j-v[k]]+w[k]),因为这种max的意思是这组可能不选任何一个,所以需要用max(dp[i][j],dp[i-1][j-v[k]+w[k])。
 
初始dp为-1,当dp=-1时这个状态是不可行的,不能转移,这个是判断是否能达到目标。
 
代码:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <queue>
  7. #include <cmath>
  8. #include <set>
  9. using namespace std;
  10.  
  11. #define N 105
  12.  
  13. int max(int x,int y){return x>y?x:y;}
  14. int min(int x,int y){return x<y?x:y;}
  15. int abs(int x,int y){return x<?-x:x;}
  16.  
  17. struct node{
  18. int v, w;
  19. };
  20.  
  21. int dp[][];
  22. vector<node>ve[];
  23. int n;
  24.  
  25. main()
  26. {
  27. int i, j, k;
  28. node p;
  29. int V;
  30. while(scanf("%d %d %d",&n,&V,&k)==){
  31. for(i=;i<=k;i++) ve[i].clear();
  32. for(i=;i<n;i++){
  33. scanf("%d %d %d",&j,&p.v,&p.w);
  34. ve[j].push_back(p);
  35. }
  36. memset(dp,-,sizeof(dp));
  37. memset(dp[],,sizeof(dp[]));
  38. for(i=;i<=k;i++){
  39. for(j=;j<ve[i].size();j++){
  40. p=ve[i][j];
  41. for(int v=V;v>=p.v;v--){
  42. if(dp[i][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i][v-p.v]+p.w);
  43. if(dp[i-][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i-][v-p.v]+p.w);
  44. }
  45. }
  46. }
  47. if(dp[k][V]==-) printf("Impossible\n");
  48. else printf("%d\n",dp[k][V]);
  49. }
  50. }

HDU 3033 分组背包变形(每种至少一个)的更多相关文章

  1. HDU 3033 分组背包(至少选一个)

    分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...

  2. HDU 3033 分组背包

    给出N个物品.M金钱.W种类 给出N个物品的性质:所属种类,花费.价值 求每一种类物品至少一个的前提下,所能购买到的最大价值 dp[i][k]表示在第i种物品.总花费为k的最大价值 dp[i][k]= ...

  3. HDU 3033 组合背包变形 I love sneakers!

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. hdu 1712 (分组背包入门)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...

  5. hdu3033 I love sneakers! 分组背包变形

    分组背包要求每一组里面只能选一个,这个题目要求每一组里面至少选一个物品. dp[i, j] 表示前 i 组里面在每组至少放进一个物品的情况下,当花费 j 的时候,所得到的的最大价值.这个状态可以由三个 ...

  6. ACboy needs your help(HDU 1712 分组背包入门)

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu3033 I love sneakers! 分组背包变形(详解)

    这个题很怪,一开始没仔细读题,写了个简单的分组背包交上去,果不其然WA. 题目分析: 分组背包问题是这样描述的:有K组物品,每组 i 个,费用分别为Ci ,价值为Vi,每组物品是互斥的,只能取一个或者 ...

  9. HDU 4341 分组背包

    B - Gold miner Time Limit:2000MS      Memory Limit:32768KB     Description Homelesser likes playing ...

随机推荐

  1. iOS改变字母的大小写

    使用lowercaseString,uppercaseString -(void)test{ NSString * str = @"person"; NSString * str1 ...

  2. background-sizi (转)

    http://www.cnblogs.com/greenteaone/archive/2012/08/28/2659878.html  (原创作者链接地址  ) Background-Size:[ & ...

  3. 我的git学习

    当遇到不想commit的,而status已经现实出来了,可以使用 git rm -r --cached "fine name or 文件夹" 出现   Git – fatal: U ...

  4. NHibernate Query

    1) Sql Group by .... 之前是这么写的,因为DateTime是YYYY-MM-DD HH:mm:SS 模式,我只想group 日期.这种写法再mysql,sqlserver.orac ...

  5. DataTable转换为Json字符串的三种方法

    //第一种:使用StringBuilder  public string DataTableToJson(DataTable table) { var JsonString = new StringB ...

  6. HTML5 中的Nav元素详解

    什么是Nav元素 Nav元素可以用作页面导航的链接组,在导航链接组里面有很多的链接,点击每个链接可以链接到其他页面或者当前页面的其他部分,并不是所有的链接组都要被放在nav元素里面,我们只需要把最主要 ...

  7. Eclipse Building Workspace 解决办法

    Eclipse 一直不停 building workspace... android开发论坛 juapk 完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭  3.maven下载l ...

  8. [ubuntu]用ubuntu开发的日子----win7 ubuntu双系统

    小子终于忍不了win7某些蛋疼的设定,看群里好多大牛推荐mac,但资金紧张,只好推而求其次使用ubuntu,但是由于公司工作环境是windows,所以还必须保留windows系统,一次决定双系统. 下 ...

  9. MySQL问题记录--python插入中文至MySQL提示SQLErroor:1366错误

    一.在爬虫脚本做以下操作仍提示错误:SQL Error: 1366: Incorrect string value: "\xd0\xc2\xce\xc5-" for column  ...

  10. Selenium ide录制回放错误Timed out after 30000ms

    [error] Timed out after 30000ms     该问题可能是速度控制条播放速度过快导致,调整播放速度至slow