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. 【SpringBoot】Spring Boot,开发社区讨论交流网站首页。

    初识Spring Boot,开发社区讨论交流网站首页. 文章目录 初识Spring Boot,开发社区讨论交流网站首页. 1.项目简介 2. 搭建开发环境 JDK Apache Maven Intel ...

  2. 【Oracle】regexp_substr()函数详解

    环境:Oracle10.2.0.5 在SQL中尝试使用正则 可以试下regexp_substr()来进行分割 首先创建一个实验视图: SQL> create or replace view te ...

  3. leetcode 473. 火柴拼正方形(DFS,回溯)

    题目链接 473. 火柴拼正方形 题意 给定一串数,判断这串数字能不能拼接成为正方形 思路 DFS,但是不能每次从从序列开始往下搜索,因为这样无法做到四个边覆盖不同位置的值,比如输入是(5,5,5,5 ...

  4. oracle创建恢复编录(recovery catalog)

    1.在要作为恢复编录的数据库创建用户 create user rman identified by oracle default tablespace system temporary TABLESP ...

  5. Vue MVVM模型原理

    最近反思了下自己,觉得自己很急躁,学技术总是觉得能用就行了,其实这样很不好,总是这样,就永远只能当用轮子的人.好了,废话不多说,转入正题: 要理解MVVM的原理,首先要理解它是什么,怎么运作起来的: ...

  6. wmic 操作文件的datafile

    wmic datafile /?动词有ASSOC,CALL,CREATE,DELETE,GET,LIST 这几个 命令:wmic datafile where "filename='dsc0 ...

  7. 如何创建一个 PostgreSQL 数据库?

    PostgreSQL 官网截图 PostgreSQL 是什么? PostgreSQL 是一个功能非常强大的,历史悠久,开源的关系数据库.PostgreSQL支持大部分的SQL标准并且提供了很多其他现代 ...

  8. C#+Layui开发后台管理系统

    ​我是笑林新记,分享一下我一套C#开发的后台管理系统,希望对大家有帮助!欢迎关注微信公众号:笑林新记   后台开发语言:C# 前端框架:layui   前天用毛笔笔画制作了一个毛笔字效果的Logo,主 ...

  9. Redis 底层数据结构设计

    10万+QPS 真的只是因为单线程和基于内存?_Howinfun的博客-CSDN博客_qps面试题 https://blog.csdn.net/Howinfun/article/details/105 ...

  10. 京东零售mockRpc实践

    https://mp.weixin.qq.com/s/A0T6ySub0DfQiXJAbWm2Qg jsf协议是基于tcp的而且对数据进行了序列化.加密等操作,直接截获的方式很难实现.最后决定注入自己 ...