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双,能获得的最大价值是多少? 思路: 分组背包的变形,变成了相反的,每组 ...
随机推荐
- Java流程控制---个人参考资料
前言:我写博客的目的很简单,很单纯,把自己平时学的东西,放到博客上,空闲的时间,就可以看看自己曾经看到过得东西. Java流程控制语句:判断结构.选择结构.循环结构 一.判断结构 判断结构包括if 分 ...
- ansible报错Aborting, target uses selinux but python bindings (libselinux-python) aren't installed【转】
报错内容: TASK [activemq : jvm configuration] ********************************************************** ...
- bzoj千题计划225:bzoj2143: 飞飞侠
http://www.lydsy.com/JudgeOnline/problem.php?id=2143 分层图最短路 把能够弹跳的曼哈顿距离看做能量 dp[i][j][k]表示在(i,j)位置,还有 ...
- c#的as,is 运算符
- HTTP 错误 404.0 - Not Found
当网上的那些修改程序池的方法,无法解决此问题时,可以尝试修改以下的参数: 1.控制面板-->程序-->启用或关闭Windows功能--> Internet Information S ...
- IL反编译的实用工具Ildasm.exe
初识Ildasm.exe——IL反编译的实用工具 https://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html 学 ...
- mybatis介绍——(一)
官方API:http://www.mybatis.org/mybatis-3/index.html 中文: http://www.mybatis.org/mybatis-3/zh/index.html ...
- flask基础之Response响应对象(九)
前言 Response对象负责对客户端的响应,每一个请求都会有一个Response对象,那么它在一个请求的声明周期内是怎么发挥作用的呢? Response对象 响应发生的位置 先回顾一下http请求的 ...
- Linux内核入门(六)—— __attribute__ 机制【转】
转自:https://blog.csdn.net/yunsongice/article/details/5538020 GNU C的一大特色(却不被初学者所知)就是__attribute__机制.__ ...
- u_boot移植之内存基础知识DDR【转】
转自:http://blog.chinaunix.net/uid-25909619-id-4938411.html