UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包)
题意分析
现在有一家人去超市购物。每个人都有所能携带的重量上限。超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个。求这一家人所能购买到的最大价值是多少。
每个人的所能携带的最大重量即为背包容量。此题只是换成n个人而已。所以分别以每个人最大携带重量为背包容量,对所有商品做01背包,求出每个人的最大价值。这些最大价值之和即为这家人购物的最大价值。
核心状态转移方程:
dp[i][j] = max(dp[i][j],dp[i-1][j-wei[i]]+val[i]);
代码总览
/*
Title:UVA.10130
Author:pengwill
Date:2017-2-16
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nvmax 35
#define nmax 1005
#define npmax 105
using namespace std;
int val[nmax],wei[nmax],dp[nmax][nvmax],pw[npmax],n,tot;
void _01kp(int pos)
{
memset(dp,0,sizeof(dp));
for(int i =1 ;i<=n;++i){
for(int j = 0;j<=pw[pos];++j){
dp[i][j] = dp[i-1][j];
if(j>=wei[i])
dp[i][j] = max(dp[i][j],dp[i-1][j-wei[i]]+val[i]);
}
}
tot+=dp[n][pw[pos]];
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
int P,W,G;
tot = 0;
scanf("%d",&n);
for(int i =1; i<=n; ++i){scanf("%d%d",&P,&W); val[i] = P; wei[i] = W;}
scanf("%d",&G);
for(int i = 1; i<=G;++i) scanf("%d",&pw[i]);
for(int i =1; i<=G;++i){
_01kp(i);
}
printf("%d\n",tot);
}
return 0;
}
UVA.10130 SuperSale (DP 01背包)的更多相关文章
- UVA 624 CD(DP + 01背包)
CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...
- POJ-1015 Jury Compromise(dp|01背包)
题目: In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- UVA 562 Dividing coins --01背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
随机推荐
- 深入理解javascript原型链
在javascript中原型和原型链是一个很神奇的东西,对于大多数人也是最难理解的一部分,掌握原型和原型链的本质是javascript进阶的重要一环.今天我分享一下我对javascript原型和原型链 ...
- 天平 (Not so Mobile UVA - 839)
题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...
- Nodejs Express笔记
Express做服务器,主要考虑到可能存在的高并发,js写起来也并不麻烦,环境搭建也异常简单.开车~ 由于主要目的就是用于生产环境,所以肯定不能用高版本的Nodejs,选LTS,没错的. 一.安装 这 ...
- POJ 2986 A Triangle and a Circle(三角形和圆形求交)
Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...
- POJ 2187 Beauty Contest(凸包+旋转卡壳)
Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...
- redis集群sentinel哨兵模式的搭建与实际应用
参考资料:https://blog.csdn.net/men_wen/article/details/72724406 之前环境使用的keepalived+redis vip集群模式,现在我们服务切换 ...
- 什么是Frozen Binary
对于Python来说,你可以将Python的字节码,PVM(也就是解析器),以及需要的相关类库,打包成一个package,这个package实际上是一个二进制可执行文件,这样,用户获取到这个packa ...
- Log Files
Description Nikolay has decided to become the best programmer in the world! Now he regularly takes p ...
- PHPCMS调取当前栏目的描述、文章位置导航、当前栏目链接、当前栏目名称
当我们填写了栏目描述,怎么调用出来. 使用 {$CATEGORYS[$catid][description]} 就可以把栏目的描述调用出来 下面三个也比较常用{catpos($catid)} 显示文章 ...
- Java 动态绑定和多态
动态绑定和多态 动态绑定是指:"在执行程序期间(而非编译期间),判断引用所指对象的实际类型,调用其相应的方法." 动态绑定(多态)存在的条件 要有继承. 要有重写. 父类引用指向子 ...