hdu 3033(好题,分组背包)
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
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.
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.
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.
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66
进行01背包就行了.我们当前选第k组的时候在里面选择物品的时候是选的第几件.如果我们当前是选第k组中的第一件,那么是从第k-1组推过来的,如果不是取第一件(i),
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(好题,分组背包)的更多相关文章
- hdu 1561 树形dp+分组背包
题意:就是给定n个点,每个地点有value[i]的宝物,而且有的宝物必须是另一个宝物取了才能取,问取m个点可以获得的最多宝物价值. 一个子节点就可以返回m个状态,每个状态表示容量为j(j<=m) ...
- 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 ...
- ACboy needs your help(HDU 1712 分组背包入门)
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- HDU 3033 分组背包变形(每种至少一个)
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3033 I love sneakers! 分组背包
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3033 分组背包(至少选一个)
分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...
- hdu 3033 I love sneakers!(分组背包+每组至少选一个)
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3033 I love sneakers! 我爱运动鞋 (分组背包+01背包,变形)
题意: 有n<=100双鞋子,分别属于一个牌子,共k<=10个牌子.现有m<=10000钱,问每个牌子至少挑1双,能获得的最大价值是多少? 思路: 分组背包的变形,变成了相反的,每组 ...
随机推荐
- linux ------ 硬连接和软连接(软连接也叫符号连接)
在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号 (Inode Index).在Linux中,多个文件名指向同一索引节点是存在的.一般这种连接就是硬连接 ...
- JavaScript--序列化以及转义
一.序列化 1.1 序列化 将其他对象转换为字符串,用法:JSON.stringify() var li = [1,2,3,4]; var new_li = JSON.stringify(li); n ...
- ideau 2018.1.2安装和使用
此博文的各安装软件.方法技巧仅供研究使用,请勿用于商业活动.下载.操作后请于24小时内删除.对于使用过程中出现的一切问题.责任.纠纷,概不负责. 1.下载ideau-2018.1.2,点击下载,提取码 ...
- POJ - 1094 Sorting It All Out(拓扑排序)
https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...
- hdu 3022 Sum of Digits
http://acm.hdu.edu.cn/showproblem.php?pid=3022 题意: 最多不超过10000组数据,每组数据给定两个数n,m,求一个最小的数,使得该数每一位之和等于n,每 ...
- 数学:二次剩余与n次剩余
二次剩余求的是这个东西 如果给定x,再给定若干个大的质数p,如果结果a相同,那么x是完全平方数? 给出别人的二次剩余的代码: /*poj 1808 题意: 判断平方剩余,即判断(x^2)%p=a是否有 ...
- 何凯文每日一句打卡||DAY13
- 【51Nod】1510 最小化序列 贪心+动态规划
[题目]1510 最小化序列 [题意]给定长度为n的数组A和数字k,要求重排列数组从而最小化: \[ans=\sum_{i=1}^{n-k}|A_i-A_{i+k}|\] 输出最小的ans,\(n \ ...
- XMPP用户登录
CHENYILONG Blog XMPP用户登录 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilon ...
- 第7月第27天 c++11 boost
1. #include <boost/asio.hpp> #include <cstdlib> #include <memory> namespace asio = ...