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

这题属于分组背包,但和多个取一个的不同,这里是至少取一个,可以设dp[i][j]表示前i组花费j元所能得到的最大价值,dp[i][j]先都初始化为-1,然后把dp[0][i]都记为0,在判断的时候要加上两个if,就是如果转移过来的状态是-1,那么这个状态已经不能成立了,所以当前这个状态肯定不能成立,这一点可以动笔画一下。

#include<stdio.h>
#include<string.h>
int max(int a,int b){
return a>b?a:b;
}
int dp[13][10060],num[13];
struct node{
int w,v;
}a[13][105];
int main()
{
int n,m,i,j,k,lei,c,d,e;
while(scanf("%d%d%d",&n,&m,&lei)!=EOF)
{
memset(a,0,sizeof(a));
memset(num,0,sizeof(num));
for(i=1;i<=n;i++){
scanf("%d%d%d",&c,&d,&e);
num[c]++;a[c][num[c]].w=d;a[c][num[c]].v=e;
}
memset(dp,-1,sizeof(dp));
//for(i=0;i<=lei;i++)dp[i][0]=0;注意:这里不能把每一行的dp[i][0]记为0,因为这样前面一行未满足至少取1个的条件是-1的话,那么这行也应该是-1,不是0.
for(i=0;i<=m;i++)dp[0][i]=0;
for(i=1;i<=lei;i++){
for(k=1;k<=num[i];k++){
for(j=m;j>=a[i][k].w;j--){
if(dp[i][j-a[i][k].w]!=-1)//这里两个if不能换顺序,因为第一个if是对这一组的多个背包进行挑选,
dp[i][j]=max(dp[i][j],dp[i][j-a[i][k].w]+a[i][k].v);//如果先进行第二个if,那么可能当前的dp[i][j]不再为-1,
if(dp[i-1][j-a[i][k].w]!=-1)//然后运行第一个if的时候可能会产生冲突,
dp[i][j]=max(dp[i][j],dp[i-1][j-a[i][k].w]+a[i][k].v);//即原来不能成立的状态变为可能然后再进行背包运算 。
}
}
}
if(dp[lei][m]<0)printf("Impossible\n");
else printf("%d\n",dp[lei][m]);
}
return 0;
}

最新文章

  1. SHA-256算法
  2. px4flow通过iic读取具体寄存器数据程序
  3. 一步步教你Hadoop多节点集群安装配置
  4. 双系统Ubuntu无法访问windows磁盘分区解决方法
  5. WP7.1 应用程序发布到Marketplace
  6. ubuntu 乱码 改为英文
  7. LA 4636 (贪心) Cubist Artwok
  8. linux-0.11内核 任务的堆栈切换
  9. [Apache系列]怎样在windows下配置apache vhost
  10. 转载:org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.Annotation
  11. tone()函数的有趣的使用案例
  12. gulp工程化工具
  13. javaWeb中MVC的编程思想示例
  14. php保留两位小数的3种方法
  15. ESB企业服务总线
  16. Numpy - Pandas - Matplot 功能与函数名 速查
  17. Maven构建Web项目
  18. easyUI取消选中的所有行
  19. HDU1069:Monkey and Banana(最长上升子序列的应用)
  20. Qt5信号和槽机制

热门文章

  1. g/test/s/lose/won/g
  2. 【Linux】服务器识别ntfs移动磁盘方法
  3. 【ASM】从asm中复制文件到本地,或者从本地到asm中方法
  4. 攻防世界—pwn—level2
  5. 网件wndr4300 ttl连接
  6. Nacos 服务配置中心
  7. JVM(八)执行引擎相关内容
  8. 一文搞定全场景K3s离线安装
  9. MySQL 压测
  10. the code has to work especially hard to keep things in the same thread