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时这个状态是不可行的,不能转移,这个是判断是否能达到目标。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 105 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} struct node{
int v, w;
}; int dp[][];
vector<node>ve[];
int n; main()
{
int i, j, k;
node p;
int V;
while(scanf("%d %d %d",&n,&V,&k)==){
for(i=;i<=k;i++) ve[i].clear();
for(i=;i<n;i++){
scanf("%d %d %d",&j,&p.v,&p.w);
ve[j].push_back(p);
}
memset(dp,-,sizeof(dp));
memset(dp[],,sizeof(dp[]));
for(i=;i<=k;i++){
for(j=;j<ve[i].size();j++){
p=ve[i][j];
for(int v=V;v>=p.v;v--){
if(dp[i][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i][v-p.v]+p.w);
if(dp[i-][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i-][v-p.v]+p.w);
}
}
}
if(dp[k][V]==-) printf("Impossible\n");
else printf("%d\n",dp[k][V]);
}
}

最新文章

  1. javascript 笔记!
  2. ndoutils2.2.0(ndo2db)中文乱码问题解决
  3. warning: incompatible implicit declaration of built-in function ‘exit’
  4. android Activity的启动模式与flag的见解
  5. 【转】Android 4.4源码下载与编译
  6. nfs:server is not responding,still trying 原因与解决
  7. Bernstein polynomials
  8. JAVA字符串比较equals()和equalsIgnoreCase()差异
  9. POI颜色设置
  10. UEP-confirm和alert弹窗
  11. spring 组件自动装载示例(@ComponentScan,@Component,@Scope)
  12. 安装Go插件遇到的问题及解决方法
  13. Scala学习笔记——断言和单元测试
  14. 43.scrapy爬取链家网站二手房信息-1
  15. C/C+ 感触
  16. IOS使用AVAudioPlayer播放mp3歌曲文件并监听来电打断
  17. PreparedStatement插入values
  18. DB2 日期时间函数
  19. 系列文章--ASP.NET之AJAX入门教程
  20. js 小复习1

热门文章

  1. WinSCP无法连接 ubuntu 的解决方法
  2. 微信小程序-视图列表渲染
  3. jsp页面输出序号
  4. Query Designer:公式冲突
  5. 谈谈Web前端工程师的定位
  6. ubuntu12.04 安装 QQ
  7. Scrum Meeting 11-20151217
  8. FFmpeg官方文档之————先进音频编码(AAC)
  9. php+js进度读取条
  10. js 时间格式化 代码