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 ...
随机推荐
- react组件性能
一.渲染原理 二.性能优化 三.Immutable在性能优化中的作用
- Git笔记——01
Git - 幕布 Git 教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...
- tomcat6升级到7时400问题,以及url带有汉字时出错。
tomcat6升级到7时400问题: 在文件catalina.properties后加入tomcat.util.http.parser.HttpParser.requestTargetAllow=|. ...
- Java应用基础微专业-设计篇
第1章--抽象与接口 1.1 抽象 An abstract class can be created without abstract methods, the purpose of doing th ...
- 简单的switch嵌套
//添加list数据 1 public static void main(String[] args) { List<String> al = new ArrayList<Strin ...
- 树莓派怎么连接无线网wifi?
没有显示器的同学,想要连接无线网,一定非常苦恼,前面教会了大家远程登录图形界面,下面我将教会大家:在没有图形界面的情况下,怎么连接树莓派WiFi.同样还是利用putty远程访问软件登录,但这次不需要登 ...
- android AVD创建
参数详解:AVD name:是要填写的虚拟机名称,这个自己随便取名就行了,要纯英文和数字组成Device:这里是要选择模拟的设备,一般选择3.2*QVGA(ADP2)(320*480: mdpi)这个 ...
- 【shell 练习5】编写简单的多级菜单
一.简单的多级菜单 [root@web129 ~]# cat menu.sh #!/bin/bash #shell菜单演示 function menu() { echo -e `date` cat & ...
- github 使用“git commit -m"命令时候出现的一个小问题
git commit -m 使用问题 今天提交文件到github,步骤是: git add abc.py (abc.py是我当前随意写的一个文件名) git commit -m 'add codes ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...