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;
}

hdu3033 I love sneakers!的更多相关文章

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

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

  2. HDU3033 I love sneakers!———分组背包

    这题的动态转移方程真是妙啊,完美的解决了每一种衣服必须买一件的情况. if(a[x][i-c[x][j].x]!=-1) a[x][i]=max(a[x][i],a[x][i-c[x][j].x]+c ...

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

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

  4. HDU-3033 I love sneakers! 题解

    题目大意 有 n 个物品,分成了 k 组,每个物品有体积和价值,把 n 个物品放到容量为 V 的背包中,保证每组至少放一件,求能获得的最大价值,如果不能实现,输出"Impossible&qu ...

  5. I love sneakers!(分组背包HDU3033)

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

  6. HD3033I love sneakers!(分组背包+不懂)

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

  7. hdu 3033 I love sneakers! 分组背包

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

  8. hdu 3033 I love sneakers!

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

  9. hdu3033I love sneakers! (分组背包,错了很多次)

    Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarshi ...

随机推荐

  1. 关于SSRF与CSRF漏洞的解释

    目录 SSRF服务端请求伪造(外网访问内网) 1.SSRF形成原因 2.利用SSRF漏洞的目的 3.SSRF漏洞的用途 4.SSRF漏洞的特性 实例 5.如何挖掘SSRF漏洞 6.常用SSRF去做什么 ...

  2. Oracle备份审计表SYS.AUD$和SYS.FGA_LOG$

    ORACLE的审计表不可以使用expdp和impdp导出和导入,如果使用,会报如下错误: 需要使用exp和imp进行导出和导出 导出语句: exp " '/ as sysdba' " ...

  3. mount: /dev/sdxx already mounted or /xxxx busy解决方法

    异常现象: 解决方法: 1.    輸入root的密碼,進入單用戶2.    重新掛載/目錄,使其變為可讀可寫 # mount –o rw,remount / 3.    修改/etc/fstab文件 ...

  4. 缓存淘汰算法 LRU 和 LFU

    LRU (Least Recently Used), 即最近最少使用用算法,是一种常见的 Cache 页面置换算法,有利于提高 Cache 命中率. LRU 的算法思想:对于每个页面,记录该页面自上一 ...

  5. 使用Logback日志

    使用Logback日志 spring boot内部使用Logback作为日志实现的框架. Logback和log4j非常相似,如果你对log4j很熟悉,那对logback很快就会得心应手. logba ...

  6. top命令详解-性能分析

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,常用于服务端性能分析. top命令说明 [www.linuxidc.com@linuxidc-t-tomcat-1 ...

  7. Property or method "previewUrl" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components,

    Property or method "previewUrl" is not defined on the instance but referenced during rende ...

  8. php artisan db:seed 报错

    在laravel 5中执行,要执行数据填充时报如下错误 php artisan db:seed 错误: [ReflectionException]                        Cla ...

  9. Golang 性能优化实战

    小结: 1. 性能查看工具 pprof,trace 及压测工具 wrk 或其他压测工具的使用要比较了解. 代码逻辑层面的走读非常重要,要尽量避免无效逻辑. 对于 golang 自身库存在缺陷的,可以寻 ...

  10. 【Coredump】调试之旅

    测试反馈,core了. 拿到环境,发现6和11,一个是重复释放,一个是非法指针. 用GDB一挂 ,发现 1 GNU gdb (GDB) 7.5 2 Copyright (C) 2012 Free So ...