HDU 3033 组合背包变形 I love sneakers!

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4503 Accepted Submission(s):
1845

Problem Description

After months of hard working, Iserlohn finally wins
awesome amount of scholarship. As a great zealot of sneakers, he decides to
spend all his money on them in a sneaker store.

There are
several brands of sneakers that Iserlohn wants to collect, such as Air Jordan
and Nike Pro. And each brand has released various products. For the reason that
Iserlohn is definitely a sneaker-mania, he desires to buy at least one product
for each brand.
Although the fixed price of each product has been labeled,
Iserlohn sets values for each of them based on his own tendency. With handsome
but limited money, he wants to maximize the total value of the shoes he is going
to buy. Obviously, as a collector, he won’t buy the same product twice.
Now,
Iserlohn needs you to help him find the best solution of his problem, which
means to maximize the total value of the products he can buy.

Input

Input contains multiple test cases. Each test case
begins with three integers 1<=N<=100 representing the total number of
products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10
representing the sneaker brands. The following N lines each represents a product
with three positive integers 1<=a<=k, b and c, 0<=b,c<100000,
meaning the brand’s number it belongs, the labeled price, and the value of this
product. Process to End Of File.

Output

For each test case, print an integer which is the
maximum total value of the sneakers that Iserlohn purchases. Print "Impossible"
if Iserlohn‘s demands can’t be satisfied.

Sample Input

5 10000 3
1 4 6
2 5 7
3 4 99
1 55 77
2 44 66

Sample Output

255

Source

2009
Multi-University Training Contest 13 - Host by HIT

分组背包问题,大意**要买鞋,有k种牌子,每种牌子至少买一双鞋子。每双鞋子有标价跟实际价值。求用m多的钱买最多价值的鞋。

其实我觉得这题的难点就在处理“至少”这点上面。

状态方程很多都能推出用 dp[k][m] 来表示 已经买了k种鞋 在有m钱状态下的 鞋的最大价值。

状态转移方程为

for( k = 1 ; k <= K ; k++)
        {
            for( i = 0 ; i < num[k] ; i++)
            {
                for( j = mm ; j >= m[k][i].m ; j--)
                {
                    if(dp[k][j - m[k][i].m] != -1)
                        dp[k][j] = Max(dp[k][j] , dp[k][j - m[k][i].m] + m[k][i].v);
                    if(dp[k-1][j - m[k][i].m] != -1 )
                        dp[k][j] = Max(dp[k][j] , dp[k-1][j - m[k][i].m] + m[k][i].v);

}
            }
        }

如果忽略了两个红色的判断句,大家都看得出这只是单纯的01背包且 没有条件限制,加了这两句就能实现至少了。理由如下

一开始我将dp[][]数组初始化为-1表示所有的数都不合法。大于0表示合法

然后将所有的 k = 0 dp[0][]置为0,这是为了 k = 1时能合法计算。

从状态方程中看出,当上一个状态值为-1时表示他不合法。所以当前状态没有计算的必要也不合法答案。

如果计算完第k类商品的取值后,所有的dp[k][]均为-1的时候,第k类表明没有一鞋被买。故所有状态都不合法,接下来的所有值也都将不合法。

在计算第k组商品的过程中,当某个-1变成一个非负数的时候,也就表明当前的第k种已经拿了第i件物品,所以变成合法答案了。

如此推下去,最后一个值dp[k][m],就是答案了。如果依然是-1,就输出impossible把。

5 13 3
1 2 3
1 4 6
2 10 2
3 2 2
3 1 2

7

m   k=0   k=1   k=2   k=3 
0    0    -1    -1    -1 
1    0    -1    -1    -1
2    0    3    -1    -1
3    0    3    -1    -1
4    0    6    -1    -1
5    0    6    -1    -1
6    0    9    -1    -1
7    0    9    -1    -1
8    0    9    -1    -1
9    0    9    -1    -1
10   0    9    -1    -1
11   0    9    -1    -1
12   0    9    5     -1
13   0    9    5      7

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
   int w;
   int v;
}que[20][20000];
int num[20];
int dp[20][20000];
int main(){
   int n,m,kk;
   while(scanf("%d%d%d",&n,&m,&kk)!=EOF){
        memset(num,0,sizeof(num));
        memset(dp,-1,sizeof(dp));
      for(int i=1;i<=n;i++){
            int x,y,z;
          scanf("%d%d%d",&x,&y,&z);
          que[x][num[x]].w=y;
          que[x][num[x]].v=z;
          num[x]++;
      }
      for(int i=0;i<=m;i++)
        dp[0][i]=0;
      for(int i=1;i<=kk;i++){
        for(int k=0;k<num[i];k++){
            for(int j=m;j>=que[i][k].w;j--){
                if(dp[i][j-que[i][k].w]!=-1)
                    dp[i][j]=max(dp[i][j],dp[i][j-que[i][k].w]+que[i][k].v);
                  if(dp[i-1][j-que[i][k].w]!=-1)
                    dp[i][j]=max(dp[i][j],dp[i-1][j-que[i][k].w]+que[i][k].v);
            }
        }
      }
      if(dp[kk][m]<0)
        printf("Impossible\n");
      else
        printf("%d\n",dp[kk][m]);
   }
   return 0;
}
时间: 2024-10-05 07:31:50

HDU 3033 组合背包变形 I love sneakers!的相关文章

HDU 3033 分组背包变形(每种至少一个)

I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4464    Accepted Submission(s): 1824 Problem Description After months of hard working, Iserlohn finally wins awesome amount of sc

HDU 3033 分组背包

http://www.hgy413.com/1319.html 简介DeviceIoControl的三种通信方式 HDU 3033 分组背包,布布扣,bubuko.com

hdu 4381(背包变形)

题意: 给定n个块,编号从1到n,以及m个操作,初始时n个块是白色. 操作有2种形式: 1 ai xi : 从[1,ai]选xi个块,将这些块涂白. 2 ai xi:从[ai,n]选xi个块,将这些块涂白. 可以忽略某些操作且如果区间内没有足够的黑块(黑块用于涂白),则不能进行这个操作. 分析: 写写画画一看就知道这道题是一个背包问题. “恰好装满背包”. 以下摘自题解: 本题难点在于正确处理两种操作,不妨假设只有一种操作,那么这种操作如果是1的话那么就把操作按照a从小到大排序,每次都尽量往最左

HDU 1203 01背包变形题,(新思路)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33303    Accepted Submission(s): 13470 Problem Description Speakless很早就想出国,现在

hdu 2184 01背包变形

转自:http://blog.csdn.net/liuqiyao_01/article/details/8753686 题意:这是又是一道01背包的变体,题目要求选出一些牛,使smartness和funness值的和最大,而这些牛有些smartness或funness的值是负的,还要求最终的smartness之和以及funness之和不能为负. 这道题的关键有两点:一是将smartness看作花费.将funness看作价值,从而转化为01背包:二是对负值的处理,引入一个shift来表 示“0”,

hdu 2670 01背包变形

题意:有n个男孩,每个男孩对女神都有一个love值Li和递减值Bi(love值每天递减这么多).女神要从这n个男孩中选出k个男孩来一起去玩耍(每天选择一个男孩),要使这k个男孩的love值之和最大. 分析:当选定的男孩一定时,肯定要尽早选择递减较快的男孩,所以先按照递减值由大到小排序,然后做01背包即可,花费是占一个人数(n个人中选择k个),价值是那一天的love值. 总结一句话就是:排序然后求一个恰好装满的01背包. 1 #include <algorithm> 2 #include <

hdu 1171 01背包变形

背景:1Y对于背包写法,不太熟,想法也不够深,写起来,容易犯小错误. 思路:把sum/2当做背包的最大容量,求这个最大容量能够装下的最大价值,这个题的灵活之处就是把价值和体积都看做题中给的价值,那么相当于,一份体积有一份价值.所以sum/2的体积产生的价值势必小于等于sum/2,这样我们求出这个最接近sum/2的值即可. 这个题的思路比较巧妙,一是把看似两方面的问题转化为单方面要接近一半的问题,而单方面接近一半的问题又把价值既当做价值又当做体积转化为01背包问题. 学习:1.定义状态:F[j]为

HDU 3466 01背包变形

给出物品数量N和总钱数M 对于N个物品.每一个物品有其花费p[i], 特殊值q[i],价值v[i] q[i] 表示当手中剩余的钱数大于q[i]时,才干够买这个物品 首先对N个物品进行 q-p的排序,表示差额最小的为最优.优先考虑放入这个物品 然后01背包计算 #include "stdio.h" #include "string.h" #include "algorithm" using namespace std; int inf=0x3f3f

hdu 3033 分组背包(每组至少选一个)

题意:有个小娃娃得了奖学金要去买东西,一共有n个东西分为k组,每个东西有一个花费和价值,问在每组东西至少买一个的条件下,小娃娃用他的奖学金买东西可以获得的最大价值. 思路:定义状态dp[i][v]表示在[1, i]组物品都至少有一个被购买时用v(背包容量)这么多钱能得到多少价值. 状态转移方程: if ( dp[i][v - cost[i][j]] != -1 ) dp[i][v] = max( dp[i][v], dp[i][v - cost[i][j]] + val[i][j] ); if