hdu 3033(好题,分组背包)

I love sneakers!

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

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

开始写始终WA,初始化有问题。。。看题解才发现原来选择的顺序也有讲究,又长见识了。

题意:一些鞋子有k种品牌,第k种牌子的鞋子里面有一些品种不同的鞋子,每种都有自己的价格和价值,收藏家有m元想收集每种品牌的鞋子至少一双(但是相同品牌相同品种的鞋他只要一双),问他总共可以收集多少价值的鞋子。

分析:有k个品牌,分组背包问题,《背包九讲》是每组最多选一件,这里不同,是选至少一件,我们对每组
进行01背包就行了.我们当前选第k组的时候在里面选择物品的时候是选的第几件.如果我们当前是选第k组中的第一件,那么是从第k-1组推过来的,如果不是取第一件(i),

那么就是第k组中的前一件(i-1)推过来的.

以下内容就是这题的精(da)华(keng)了:

但是这里有个非常重要的问题,我们写成要写成如下形式两个if不能调转:
if(dp[k][v-price[i]] != -1)
        dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
if(dp[k-1][v-price[i]] != -1 )
        dp[k][v] = max(dp[k][v] , dp[k-1][v-price[i]] + value[i]);
解释:顺序不能调转,因为如果代价为0,调转的话,有可能出现先有dp[k][v] = dp[k-1][v-0]+v,再有dp[k][v] =dp[k][v-0]+v = dp[k-1][v-0]+v+v,所以物品取了两次.
当然一种方便的解决办法是直接写个函数 a = max(a,b,c);
还有个重要的地方是初始化:如果我们一开始把dp初始化为0,则当所有鞋子的价值都是0时,我们就无法区分是买不全那几款鞋子还是能买全但最大价值是0

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10005
using namespace std;

int dp[105][N];  ///dp[k][i] 代表在前k组中花费i取得的最大价值
int a[105],price[105],value[105];
int main()
{
    int n,m,K;
    while(scanf("%d%d%d",&n,&m,&K)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&a[i],&price[i],&value[i]);
        }
        memset(dp,-1,sizeof(dp)); ///-1表示状态不合法
        for(int i=0; i<=m; i++) dp[0][i]=0;
        for(int k=1; k<=K; k++)
        {
            for(int i=1; i<=n; i++)
            {
                if(a[i]==k)
                    for(int v=m; v>=price[i]; v--)
                    {
                        if(dp[k][v-price[i]] != -1)
                            dp[k][v] = max(dp[k][v] , dp[k][v - price[i]] + value[i]);
                        if(dp[k-1][v-price[i]] != -1 )
                            dp[k][v] = max(dp[k][v] , dp[k-1][v-price[i]] + value[i]);
                    }
            }
        }
        if(dp[K][m]==-1) printf("Impossible\n");
        else printf("%d\n",dp[K][m]);
    }
    return 0;
}
时间: 2024-08-02 06:28:49

hdu 3033(好题,分组背包)的相关文章

hdu 1561 树形dp+分组背包

题意:就是给定n个点,每个地点有value[i]的宝物,而且有的宝物必须是另一个宝物取了才能取,问取m个点可以获得的最多宝物价值. 一个子节点就可以返回m个状态,每个状态表示容量为j(j<=m)时选最多的宝物,而一个子节点中只可以选择一个状态进行转移,每个节点有若干个子节点,问题就转换为分组背包,几个子节点就是几个分组背包,体积是选几个地点,价值是宝物价值. 状态转移方程: dp[v][1] = Money[v]; (v为叶子节点)                    dp[v][j] = m

HDU 4341 Gold miner(分组背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意: 一个人在原点(0,0)抓金子,每块金子是二维坐标平面的一个点(x,y)且y>0. 每块金子有一个价值v和获得需要的时间t.如果多个金子在一条从原点射出的直线上,那只能先抓近的,再抓远的.求在给定时间T下,所能获得的最大价值. 分析: 首先想想如果所有点都不共线是什么情况? 就是在给定时间T内要获取最大的价值和的点, 且所有点都可以任意选取. 那么这就是一个01背包问题. 不过如果存在共线的点,那

HDU 4341 Gold miner 分组背包变形

题意: 挖金矿,人在(0,0)点 n个金子 游戏时间为T 下面n行 (x, y) cost val 且若人 和 多块金子共线时,只能先取最近的金子,依次取,就是和游戏一样的. 且所有点只在1 2象限 思路: 我们先把所有共线的点分组 对于每组的物品,我们都可以认为取这个物品的花费就是前面所有物品的花费总和,而价值就是前面所有物品的价值总和. 这样就能消除每组物品的先取后取的影响了,但有一个情况就是这组至多只能取一个物品,所以每个状态只能是取了这个物品后或者是原始状态. 用原始状态转移,然后和原始

hdu 4003 树形dp+分组背包

题意:求K个机器人从同一点出发,遍历所有点所需的最小花费 链接:点我 Sample Input 3 1 1 //3个点,从1出发,1个机器人 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1 Sample Output 3 2 转移方程: dp[i][j]=min(dp[i][j],dp[i][j*k],dp[son[i]][k]+len(i,son[i])*k) 方程还是比较好写的,主要是要遍历所有的点 下面我们分析一下第一个样例 1 / \ / \ 2 3 我们派了一个机器人去3,

hdu 4003 树形dp+分组背包 2011大连赛区网络赛C

题意:求K个机器人从同一点出发,遍历所有点所需的最小花费 链接:点我 Sample Input 3 1 1 //3个点,从1出发,1个机器人 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1 Sample Output 3 2 转移方程: dp[i][j]=min(dp[i][j],dp[i][j*k],dp[son[i]][k]+len(i,son[i])*k) 方程还是比较好写的,主要是要遍历所有的点 下面我们分析一下第一个样例 1 / \ / \ 2 3 我们派了一个机器人去3,

hdu4003 树形dp+分组背包

http://acm.hdu.edu.cn/showproblem.php?pid=4003 Problem Description Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on

hdu 5148 树形dp+分组背包问题

http://acm.hdu.edu.cn/showproblem.php?pid=5148 Problem Description Long long ago,there is a knight called JayYe.He lives in a small country.This country is made up of n cities connected by n-1 roads(that means it's a tree).The king wants to reward Ja

HDU 3033 分组背包

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

HDU 3033 I love sneakers! 分组背包

我是个逗比...真心不是搞算法的料 不太中规中矩的分组背包,分组至少选一件商品.dp[i][j] 可以由当前dp[i-1][j-c] 和 dp[ i ][j-c]更新得到. #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <cmath