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 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
 
 
题目意思:
有n种物品,每种可能有多个,每个有不同体积和价值。初始手中有V的背包,现将所有种类至少一个装进背包中,若无法达到目标输出Impossible,否则输出最大的价值。
 
思路:
原始的分组背包问题是每种选或不选,而且选的话只能选一个。而这道题是每种必选,选的话可以选多个。
选多个的条件很容易,把分组背包for V 和 for 第i个  互换一下层数即可。
必选的条件有点难度,若必选的话,那么dp[i][j]由dp[i][j-v[k]]和dp[i-1][j-v[k]]转移即 在第i组中去掉v[k]体积和在前i-1组中去掉v[k]体积,在取max时不能用max(dp[i-1][j],dp[i-1][j-v[k]]+w[k]),因为这种max的意思是这组可能不选任何一个,所以需要用max(dp[i][j],dp[i-1][j-v[k]+w[k])。
 
初始dp为-1,当dp=-1时这个状态是不可行的,不能转移,这个是判断是否能达到目标。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 105 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} struct node{
int v, w;
}; int dp[][];
vector<node>ve[];
int n; main()
{
int i, j, k;
node p;
int V;
while(scanf("%d %d %d",&n,&V,&k)==){
for(i=;i<=k;i++) ve[i].clear();
for(i=;i<n;i++){
scanf("%d %d %d",&j,&p.v,&p.w);
ve[j].push_back(p);
}
memset(dp,-,sizeof(dp));
memset(dp[],,sizeof(dp[]));
for(i=;i<=k;i++){
for(j=;j<ve[i].size();j++){
p=ve[i][j];
for(int v=V;v>=p.v;v--){
if(dp[i][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i][v-p.v]+p.w);
if(dp[i-][v-p.v]!=-) dp[i][v]=max(dp[i][v],dp[i-][v-p.v]+p.w);
}
}
}
if(dp[k][V]==-) printf("Impossible\n");
else printf("%d\n",dp[k][V]);
}
}

HDU 3033 分组背包变形(每种至少一个)的更多相关文章

  1. HDU 3033 分组背包(至少选一个)

    分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...

  2. HDU 3033 分组背包

    给出N个物品.M金钱.W种类 给出N个物品的性质:所属种类,花费.价值 求每一种类物品至少一个的前提下,所能购买到的最大价值 dp[i][k]表示在第i种物品.总花费为k的最大价值 dp[i][k]= ...

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

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  4. hdu 1712 (分组背包入门)

    http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...

  5. hdu3033 I love sneakers! 分组背包变形

    分组背包要求每一组里面只能选一个,这个题目要求每一组里面至少选一个物品. dp[i, j] 表示前 i 组里面在每组至少放进一个物品的情况下,当花费 j 的时候,所得到的的最大价值.这个状态可以由三个 ...

  6. ACboy needs your help(HDU 1712 分组背包入门)

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu3033 I love sneakers! 分组背包变形(详解)

    这个题很怪,一开始没仔细读题,写了个简单的分组背包交上去,果不其然WA. 题目分析: 分组背包问题是这样描述的:有K组物品,每组 i 个,费用分别为Ci ,价值为Vi,每组物品是互斥的,只能取一个或者 ...

  9. HDU 4341 分组背包

    B - Gold miner Time Limit:2000MS      Memory Limit:32768KB     Description Homelesser likes playing ...

随机推荐

  1. js对象/数组深度复制

    今天碰到个问题,js对象.数组深度复制:之前有见过类似的,不过没有实现函数复制,今晚想了一下,实现代码如下: function clone(obj) { var a; if(obj instanceo ...

  2. tomcat部署web项目的3中方法

    1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法. 2.在tomcat安装目录中有一个conf文件夹,打开此文件夹,其 ...

  3. iOS中iconfont(图标字体)的基本使用

    前言 近日在做项目时,项目组有提出iconfont的技术,便开始查询相关资料.iconfont技术的主要目的是为减少应用体积而生.首先icon代表图标 font代表字体.此技术便是将图标转化为字体,从 ...

  4. 参考__Larvel

    环境搭建 Vagrant PSR规范 PHP 标准规范(中文版)PHP 标准规范(Github版)PHP 标准规范官网 社区 laravel-china官网Larvel学院

  5. 参考__MySql

    博客 三范式 事务隔离级别 列表

  6. linux下使用多线程编写的聊天室

    自从开始学linux网络编程后就想写个聊天室,一开始原本打算用多进程的方式来写,可是发觉进程间的通信有点麻烦,而且开销也大,后来想用多线程能不能实现呢,于是便去看了一下linux里线程的用法,实际上只 ...

  7. duilib的caption上的Edit无法激活

    当窗口设置标题栏时,鼠标等控件可以相应,edit无法响应. 主要和WindowImplBase::OnNcHitTest 虚函数有关. LRESULT WindowImplBase::OnNcHitT ...

  8. OneProxy自动剔除延迟节点

    在多种情况下,MySQL主从节点之间可能存在延迟.比如,主库的写入能力强于从库的写入能力.从库单线程复制.从库复制出错导致相关进程停止.为了保证数据的时效性,OneProxy提供了复制时效性检查,用于 ...

  9. 如何让nginx显示文件夹目录

    1. 如何让nginx显示文件夹目录 vi /etc/nginx/conf.d/default.conf 添加如下内容: location / {           root /data/www/f ...

  10. 可以结合react的ui组件

    https://ant.design/components/switch-cn/