I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4919    Accepted Submission(s): 2022

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
开始写始终WA,初始化有问题。。。看题解才发现原来选择的顺序也有讲究,又长见识了。
题意:一些鞋子有k种品牌,第k种牌子的鞋子里面有一些品种不同的鞋子,每种都有自己的价格和价值,收藏家有m元想收集每种品牌的鞋子至少一双(但是相同品牌相同品种的鞋他只要一双),问他总共可以收集多少价值的鞋子。
分析:有k个品牌,分组背包问题,《背包九讲》是每组最多选一件,这里不同,是选至少一件,我们对每组
进行01背包就行了.我们当前选第k组的时候在里面选择物品的时候是选的第几件.如果我们当前是选第k组中的第一件,那么是从第k-1组推过来的,如果不是取第一件(i),
那么就是第k组中的前一件(i-1)推过来的.
以下内容就是这题的精(da)华(keng)了:
但是这里有个非常重要的问题,我们写成要写成如下形式两个if不能调转:
if(dp[k][v-price[i]] != -1)
        dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
if(dp[k-1][v-price[i]] != -1 )
        dp[k][v] = max(dp[k][v] , dp[k-1][v-price[i]] + value[i]);
解释:顺序不能调转,因为如果代价为0,调转的话,有可能出现先有dp[k][v] = dp[k-1][v-0]+v,再有dp[k][v] =dp[k][v-0]+v = dp[k-1][v-0]+v+v,所以物品取了两次.
当然一种方便的解决办法是直接写个函数 a = max(a,b,c);
还有个重要的地方是初始化:如果我们一开始把dp初始化为0,则当所有鞋子的价值都是0时,我们就无法区分是买不全那几款鞋子还是能买全但最大价值是0
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10005
using namespace std; int dp[][N]; ///dp[k][i] 代表在前k组中花费i取得的最大价值
int a[],price[],value[];
int main()
{
int n,m,K;
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d%d%d",&a[i],&price[i],&value[i]);
}
memset(dp,-,sizeof(dp)); ///-1表示状态不合法
for(int i=; i<=m; i++) dp[][i]=;
for(int k=; k<=K; k++)
{
for(int i=; i<=n; i++)
{
if(a[i]==k)
for(int v=m; v>=price[i]; v--)
{
if(dp[k][v-price[i]] != -)
dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
if(dp[k-][v-price[i]] != - )
dp[k][v] = max(dp[k][v] , dp[k-][v-price[i]] + value[i]);
}
}
}
if(dp[K][m]==-) printf("Impossible\n");
else printf("%d\n",dp[K][m]);
}
return ;
}

hdu 3033(好题,分组背包)的更多相关文章

  1. hdu 1561 树形dp+分组背包

    题意:就是给定n个点,每个地点有value[i]的宝物,而且有的宝物必须是另一个宝物取了才能取,问取m个点可以获得的最多宝物价值. 一个子节点就可以返回m个状态,每个状态表示容量为j(j<=m) ...

  2. hdu 4003 树形dp+分组背包 2011大连赛区网络赛C

    题意:求K个机器人从同一点出发,遍历所有点所需的最小花费 链接:点我 Sample Input 3 1 1 //3个点,从1出发,1个机器人 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1 ...

  3. ACboy needs your help(HDU 1712 分组背包入门)

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU 1712 ACboy needs your help (分组背包模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...

  5. HDU 3033 分组背包变形(每种至少一个)

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

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

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

  7. HDU 3033 分组背包(至少选一个)

    分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...

  8. hdu 3033 I love sneakers!(分组背包+每组至少选一个)

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

  9. HDU 3033 I love sneakers! 我爱运动鞋 (分组背包+01背包,变形)

    题意: 有n<=100双鞋子,分别属于一个牌子,共k<=10个牌子.现有m<=10000钱,问每个牌子至少挑1双,能获得的最大价值是多少? 思路: 分组背包的变形,变成了相反的,每组 ...

随机推荐

  1. vim编辑器基本操作介绍

    vim编辑器基本操作介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 可能很多小伙伴都听说过vi编辑器或是vim编辑器.它们是Unix和Linux世界最流行的编辑器之一,他们的特 ...

  2. 数据结构(三)串---BF算法(朴素模式匹配)

    (一)BF算法了解 BF算法,即暴风(Brute Force)算法,是普通的模式匹配算法.BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T ...

  3. [转载]Cross-Platform Development in Visual Studio

    http://msdn.microsoft.com/en-us/library/dn771552.aspx http://www.cnblogs.com/mengkzhaoyun/p/4152823. ...

  4. [HAOI2008]移动玩具(状压&带权二分图)

    题目描述 • 一个 4 × 4 的 0/1 矩阵 • 每次可以交换相邻两个元素 • 求从初始状态到目标状态的最小交换次数 输入格式 前四行,每行一个长为 4 的 0/1 字符串,描述初始状态. 后四行 ...

  5. 关于v4包的Fragment过渡动画的事件监听无响应问题解决

    项目中部分功能模块采用了单Activity+多Fragment模式,当Fragment切换时,需要在过渡动画执行完后做一些操作,通常就是在自己封装的FragmentBase中重写onCreateAni ...

  6. 【转】2019年3月 最新win10激活密匙 win10各版本永久激活序列号 win10正式版激活码分享

    现在市面上大致有两种主流激活方法,一种是通过激活码来激活,另外一种是通过激活工具来激活.但是激活工具有个弊端就是激活时间只有180天,很多网友都想要永久激活,现在已经过了win10系统免费推广期了,所 ...

  7. 基于theano的多层感知机的实现

    1.引言 一个多层感知机(Multi-Layer Perceptron,MLP)可以看做是,在逻辑回归分类器的中间加了非线性转换的隐层,这种转换把数据映射到一个线性可分的空间.一个单隐层的MLP就可以 ...

  8. 【算法】Huffman编码(数据结构+算法)

    1.描述 Huffman编码,将字符串利用C++编码输出该字符串的Huffman编码. Huffman树是一种特殊结构的二叉树,由Huffman树设计的二进制前缀编码,也称为Huffman编码在通信领 ...

  9. Android 中关于 【Cursor】 类的介绍

    转自(http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html) 使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生 ...

  10. Django Rest Framework-APIView源码分析

    class APIView(View): # The following policies may be set at either globally, or per-view. renderer_c ...