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. MySQL select join on 连表查询和自连接查询

    连表查询 JOIN ON 操作 描述 inner join 只返回匹配的值 right join 会从右表中返回所有的值, 即使左表中没有匹配 left join 会从左表中返回所有的值, 即使右表中 ...

  2. ORA-00245 control file backup operation failed 分析和解决

    一.问题说明 操作系统: RedHat 5.8 数据库: 11.2.0.3 2节点RAC. 使用RMAN 备份的时候,报如下错误: ORA-00245: control file backup fai ...

  3. XSS - Labs 靶场笔记(下)

    Less - 11: 1.观察界面和源代码可知,依旧是隐藏表单 2.突破点是 $str11=$_SERVER['HTTP_REFERER']; (本题为HTTP头REFERER注入) 3.因此构造pa ...

  4. pod管理调度约束、与健康状态检查

    pod的管理 [root@k8s-master ~]# vim pod.yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: ...

  5. Poj-P2533题解【动态规划】

    本文为原创,转载请注明:http://www.cnblogs.com/kylewilson/ 题目出处: http://poj.org/problem?id=2533 题目描述: 如果ai1 < ...

  6. Linux防火墙和iptables

    1. CentOS 查看防火墙状态: systemctl status firewalld firewall-cmd --state 启停防火墙: # 开启 systemctl start firew ...

  7. python生成器 递归

    生成器 生成器:只要函数体内出现yield关键字,那么再执行函数就不会执行函数代码,会得到一个结果,该结果就是生成器   生成器就是迭代器   yield的功能 1.yield为我们提供了一种自定义迭 ...

  8. 开源AwaitableCompletionSource,用于取代TaskCompletionSource

    1 TaskCompletionSource介绍 TaskCompletionSource提供创建未绑定到委托的任务,任务的状态由TaskCompletionSource上的方法显式控制,以支持未来的 ...

  9. 【LinuxShell】free 命令详解

    前言 free命令用来显示Linux中的内存使用信息,包括空闲的.已用的物理内存,swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 命令格式 ...

  10. loj10103电力

    题目描述 原题来自:CTU Open 2004 求一个图删除一个点之后,联通块最多有多少. 输入格式 多组数据.第一行两个整数 P,C  表示点数和边数.接下来 C 行每行两个整数 ,表示 P1 与 ...