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

大致题意:

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

思路:

为了能够体现篮子的价值,我们可以多加一维表示当前买第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. web调试代理工具Whistle

    由于最近在学习微信小程序开发,项目中用到了https代理请求,所以用到了基于Node实现的跨平台web调试代理工具Whistle,在此做一记录. 完成https代理请求总共需要5个步骤. 一.安装No ...

  2. MIT 6.824学习笔记2 RPC/Thread

    本节内容:Lect 2   RPC and Threads 线程:Threads allow one program to (logically) execute many things at onc ...

  3. jQuery easyUI 使用 datagrid 表格

    获取后台数据依旧是使用一般处理程序(ashx) ,分页上添加一个函数(pagerFilter(data)) 前端代码: <%@ Page Language="C#" Auto ...

  4. vue,一路走来(4)--vuex

    补充 调用外部js,详细介绍如何调用函数. 1.首先在main.js里引用文件 2.然后算是和jquery框架一样需要所谓的入口函数吧 不过令我烦恼的是,在应用的文件中需要把他包含在另一个函数里,才可 ...

  5. 条款2:尽量使用const ,enum,inline替换define

    宁可使用编译器而不用预处理器 假设我们使用预处理器: #define ABC 1.56 这标识符ABC也许编译器没看到,也许它在编译器处理源码前就被预处理器移走了,于是“标识符”ABC没有进入标识符列 ...

  6. Kotlin——关于字符串(String)常用操作汇总

    在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin——初级篇(三):数据类型详解这篇文章. 在 ...

  7. Go copy 的使用

    copy 可以将后面的 第2个切片的元素赋值copy 到第一个切片中 package main; import "fmt" func test () { s1 := []int{1 ...

  8. bzoj4903 & loj2264 [Ctsc2017]吉夫特 Lucas 定理+状压DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4903 https://loj.ac/problem/2264 http://uoj.ac/pr ...

  9. springBoot 连接数据库

    心得:1.先添加依赖. 2.在application.yml文件中创建mybatis的连接,与项目连接起来. 3.application.yml文件中可以自行配置服务器的端口,配置mybatis的路径 ...

  10. poj 2186: Popular Cows(tarjan基础题)

    题目链接 tarjan参考博客 题意:求在图上可以被所有点到达的点的数量. 首先通过tarjan缩点,将所有内部两两可达的子图缩为一点,新图即为一个有向无环图(即DAG). 在这个DAG上,若存在不止 ...