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 ...
随机推荐
- 容器云技术:容器化微服务,Istio占C位出道
在精彩的软件容器世界中,当新项目涌现并解决你认为早已解决的问题时,这感觉就像地面在你的脚下不断地移动.在许多情况下,这些问题很久以前被解决,但现在的云原生架构正在推动着更大规模的应用程序部署,这就需要 ...
- Katalon 学习笔记(一)
工具介绍: Katalon Studio是一个能提供一整套功能来实现Web,API和Mobile的全自动测试解决方案的自动化测试平台.Katalon Studio构建于开源Selenium和App ...
- 在deepin系统中制作桌面快捷方式
在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...
- lintcode172 删除元素
删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个数组 [0, ...
- 352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- jQuery实现checkbox(复选框)选中、全选反选代码
谁都知道 在html 如果一个复选框被选中 是 checked="checked". 但是我们如果用jquery alert($("#id").attr(&qu ...
- selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python
我们在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中 ...
- Rightmost Digit(最后一位数字)
Description Given a positive integer N, you should output the most right digit of N^N. Input The ...
- 2017-2018-2 20172323 『Java程序设计』课程 结对编程练习_四则运算 2
相关过程截图 关键代码解释 将运算式分开的代码 String[] result = num.split("\\s"); 将输入的num以空格为间隔符号分开,将每一个间隔开的字符存入 ...
- python 项目配置虚拟环境
# Windows 环境1, 安装 Visual C++ 2015 Build Tools, 依赖.Net Framework 4.6, 安装包位置 ./tools/windows/visualcpp ...