这个是一个背包的变形题,很值得仔细体味

大致题意:

这个比普通背包多一个限制:再选每一类物品之前必须要先购买一个篮子来装,篮子有一定的价格,其他就和背包是一样的了

思路:

为了能够体现篮子的价值,我们可以多加一维表示当前买第i个篮子和不买的情况

dp[i][j][0]表示用j个金币且第i个物品篮子没有买的前提下能获得最多的价值

dp[i][j][1]表示用j个金币且第i个物品篮子已经买的前提下能获得最多的价值

那么状态建好了,下面试着进行状态转移

dp[i][j][0] = max(dp[i-1][j][0], dp[i-1][j][1]);

dp[i][j][1]的状态转移稍微复杂一点,因为第i类物品有好几个,这个时候我们就要对i类每一个物品进行01选择

设当前物品价格p价值v

dp[i][j][1] = max(dp[i][j][1], dp[i][j-c-p][0]+v, dp[i][j-p][1] + v);

哈哈多么漂亮机智的移到

Description

FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds of stuff (that is to say, if he is going to buy one
of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money. 
 

Input

The first line will contain two integers, n (the number of boxes 1 <= n <= 50), w (the amount of money FJ has, 1 <= w <= 100000) Then n lines follow. Each line contains the following number pi (the price of the ith box 1<=pi<=1000),
mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000) 
 

Output

For each test case, output the maximum value FJ can get 
 

Sample Input

3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60
 

Sample Output

210
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector> using namespace std;
const int maxn = 1e5+100;
const int INF = 0x7f7f7f7f;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
#define ll long long
int max(int a,int b,int c){
return max(max(a,b),c);
}
int dp[maxn][2], p, c, v, num;
int main(){
#ifdef LOCAL
freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
#endif
int n, m, k;
while(scanf("%d%d", &n, &m)==2){
memset(dp,0,sizeof dp);
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &p, &num);
for(int j = 0; j <=m; ++j){
dp[j][0] = max(dp[j][0], dp[j][1]);
if(j < p) dp[j][1] = -INF;
else dp[j][1] = 0;
}
for(int j = 1; j <= num; ++j) {
scanf("%d%d",&c, &v);
for(int j = m; j >= 0; --j) {
if(j >= p + c)
dp[j][1] = max(dp[j-c-p][0] + v, dp[j-c][1] + v, dp[j][1]);
}
}
}
cout << max(dp[m][1], dp[m][0]) << "\n";
}
return 0;
}

HDU3449_Consumer的更多相关文章

随机推荐

  1. python学习第十五天集合的创建和基本操作方法

    集合是python独有的数据列表,集合可以做数据分析,集合是一个无序的,唯一的的数据类型,可以确定列表的唯一性,说一下集合的创建和基本常见操作方法 1,集合的创建 s={1,2,4} 也可以用set( ...

  2. php中的花括号使用详解

    1.简单句法规则(用花括号界定变量名,适用于PHP所有版本,是php系统设定):    $a = 'flower';    echo "She received some $as" ...

  3. 2018-5-20-C#-BBcode-转-Markdown

    title author date CreateTime categories C# BBcode 转 Markdown lindexi 2018-05-20 14:58:57 +0800 2018- ...

  4. 6层PCB设计技巧和步骤

    6层PCB设计技巧和步骤 一.原理图的编辑  6层板由于PCB板中可以有两层地,所以可以将模拟地和数字地分开.对于统一地还是分开地,涉及到电磁干扰中信号的最小回流路径问题,绘制完原理图,别忘检查错误和 ...

  5. 2019西北工业大学程序设计创新实践基地春季选拔赛 D(卢卡斯定理)

    链接:https://ac.nowcoder.com/acm/contest/553/D来源:牛客网 Chino with Equation 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  6. (ACM模板)不定长数组vector

    #include<iostream> #include<cstdio> #include<vector> #include<algorithm> usi ...

  7. HDU4089/Uva1498 Activation 概率DP(好题)

    题意:Tomato要在服务器上激活一个游戏,一开始服务器序列中有N个人,他排在第M位,每次服务器会对序列中第一位的玩家进行激活,有四种结果: 1.有p1的概率会激活失败,这时候序列的状态是不变的.2. ...

  8. rabbit 独占队列

    std::string queue_name = "hello"; AmqpClient::Channel::ptr_t channel = AmqpClient::Channel ...

  9. python多个装饰器

    '''在装饰器中加上参数:1.实现在源代码中加上时间统计:函数timmer2.实现用户名认证功能:函数auth23.实现一次认证,刷新后自动登录功能,index函数已经认证并登录,在执行home函数时 ...

  10. ssm框架错误展示-1

    1.xmlParse错误+找不到bean ,一般会出现在导入别的地方拷来的项目时发生,报了以下错误: 查看自己的项目配置文件,发现spring的配置文件和sql映射xml文件都有个黄颜色的感叹号,鼠标 ...