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背包)的更多相关文章

  1. 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 ...

  2. POJ-1015 Jury Compromise(dp|01背包)

    题目: In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting ...

  3. USACO Money Systems Dp 01背包

    一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...

  4. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  5. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  6. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  9. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

随机推荐

  1. 后续博客转移到zhylj.cc

    此博客暂不更新了 zhylj.cc

  2. cf#516C. Oh Those Palindromes(最多回文子串的字符串排列方式,字典序)

    http://codeforces.com/contest/1064/problem/C 题意:给出一个字符串,要求重新排列这个字符串,是他的回文子串数量最多并输出这个字符串. 题解:字典序排列的字符 ...

  3. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  4. php常用几个数组的区别

    本文主要介绍的php数组函数主要有:sort.rsort.asort.arsort.ksort.krsort 测试数据定义一个关联数组如下: $data=[ 'f'=>123, 'b'=> ...

  5. Linux搭建mysql、apache、php服务总结

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 04 月 22 日 原地址:https://niaobulashi.com/archives/linux-mysql-apache- ...

  6. lintcode First Unique Number In Stream

    First Unique Number In Stream 描述: Given a continuous stream of numbers, write a function that return ...

  7. LeetCode 120——三角形最小路径和

    1. 题目 2. 解答 详细解答方案可参考北京大学 MOOC 程序设计与算法(二)算法基础之动态规划部分. 从三角形倒数第二行开始,某一位置只能从左下方或者右下方移动而来,因此,我们只需要求出这两者的 ...

  8. 自测之Lesson5:标准I/O

    题目:使用perror函数和strerror函数编写一个程序. 程序代码: #include <stdio.h> #include <errno.h> #include < ...

  9. 第一课——从main到WinMain

    一.Visual C++6.0 由微软公司推出的基于Windows系统的可视化集成开发环境(IDE) 微软公司为其开发了功能强大的MFC(Microsoft Foundation Class,微软基础 ...

  10. Java版office文档在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...