C - I love sneakers!

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

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
 题意:一个人去买运动鞋,他手里拥有的钱为m,运动鞋有k种品牌,每种品牌都可能有多双运动鞋,并且对于每种品牌,他都至少要买一双。给出n双运动鞋,运动鞋的品牌号,价格,价值分别为a,b,c。问他用那些钱去买运动鞋,能获得的最大价值是多少。
思路:

1. 要求每一组之中至少有一个被选中,和之前的最多有一个稍有区别,需要把题目再次细分。

2. dp[j][w] 表示选定 j 个品牌并且花费w的最大价值,dp[j]w] 为正数表示状态存在,为负数表示状态不存在。

3. dp[j][w] = max(dp[j][w], dp[j][w - s[i].b] +s[i].c);  第j类品牌有选择并且要选择第i件物品。(不选择第i件物品是相等的,所以略过转移方程)

dp[j][w] = max(dp[j][w], dp[j - 1][w- s[i].b] +s[i].c);  第j类品牌前面没有选择并且要选择第i件物品。

4. 由于状态是从 0 到 i 的且 dp[0][w] = 0,其他为 -INF。所以只有当第一类品牌的状态存在时,才能推导出来第二类品牌的存在状态,以此类推。

5. 题目中有 2 个陷阱,一是可能会存在某品牌数量为 0 的情况,另外会存在费用或价格为 0 的情况,所以状态转移方程不能调换。

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x7fffffff using namespace std;
struct ks{
int id;
int b;
int c;
};
ks s[]={};
int dp[][]={};
int number[]={};
bool cmp(ks a1,ks a2){
if(a1.id==a2.id)
return a1.b<a2.b;
else
return a1.id<a2.id;
} int main()
{
freopen("1.txt","r",stdin);
int n,money,k;
int a;
int i,j;
while(cin>>n>>money>>k){
for(i=;i<n;i++){
cin>>s[i].id>>s[i].b>>s[i].c;//录入信息
}
sort(s,s+n,cmp);//按产品号和价格的升序进行排列
for(i=;i<k;i++){
for(j=;j<=money;j++)
dp[i][j]=-INF;
}
dp[][]=;//重置为0,为了第一重循环时的使用
int w;
j=;
for(i=;i<n;i++){
if(i&&s[i-].id!=s[i].id)
j++;//记录鞋的品牌种数
for(w=money;w>=s[i].b;w--)//一定要从后向前推否则会取重的
{//技巧性的实现了至少购买1双鞋子
if(dp[j][w-s[i].b]!=-INF&&dp[j][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子已经买过了,并且买下第i双鞋子;
dp[j][w]=dp[j][w-s[i].b]+s[i].c;
if(j&&dp[j-][w-s[i].b]!=-INF&&dp[j-][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子没有买过,并且买下第i双鞋子;
dp[j][w]=dp[j-][w-s[i].b]+s[i].c;
}
}
int ans=-;
for(i=money;i>=;i--){
if(ans<dp[j][i])
ans=dp[j][i];
}
if(ans==-||j<k-)
cout<<"Impossible"<<endl;
else
cout<<ans<<endl;
}
return ;
}

***C - I love sneakers!(动态规划,分组背包)的更多相关文章

  1. [HDU 3033] I love sneakers! (动态规划分组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3033 题意:给你K种品牌,每种品牌有不同种鞋,现在每种品牌至少挑一款鞋,问获得的最大价值,如果不能每种 ...

  2. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

  3. 【HDU】I love sneakers!(分组背包)

    看了许多的题解,都有题目翻译,很不错,以后我也这样写.直接翻译样例: /*鞋子的数量N[1, 100]; 拥有的金钱M[1, 1w]; 品牌数目[1, 10]*/ /*以下四行是对于每双鞋的描述*/ ...

  4. BZOJ1296 [SCOI2009]粉刷匠 动态规划 分组背包

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1296 题意概括 有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝 ...

  5. I love sneakers!(分组背包HDU3033)

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

  6. hdu 3033 I love sneakers! 分组背包

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

  7. CJOJ 2040 【一本通】分组背包(动态规划)

    CJOJ 2040 [一本通]分组背包(动态规划) Description 一个旅行者有一个最多能用V公斤的背包,现在有n件物品,它们的重量分别是W1,W2,...,Wn,它们的价值分别为C1,C2, ...

  8. hdu 3033 I love sneakers!(分组背包+每组至少选一个)

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

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

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

  10. HDU3033I love sneakers!(分组背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3033 本题的意思就是说现在有n种牌子的鞋子,每种品牌有一些不同的鞋,每双鞋子都有一个特定的权值,现在要求每种品牌 ...

随机推荐

  1. USACO 3.2 Magic Squares

    Magic SquaresIOI'96 Following the success of the magic cube, Mr. Rubik invented its planar version, ...

  2. sulime text3

    sublime text 3 详细说明--包括快捷键 sublime 插件安装 快捷键 sunlime (需要先安装package control,ctrl+shift+p,输入insall之后安装插 ...

  3. Asp.net简单代码设置GridView自适应列宽不变形

    动态绑定的GridView由于列数不固定,而列又太多,是要自定设置gridView的宽度 //在GridView的行数据绑定完的事件中设置 protected void gvObjectList_Ro ...

  4. C# 语言规范_版本5.0 (第18章 不安全代码)

    1. 不安全代码 **(注:此章对于跨多语言编程开发非常重要,如遇异常无法完成跨语言,建议使用此种方式.) 如前面几章所定义,核心 C# 语言没有将指针列入它所支持的数据类型,从而与 C 和 C++ ...

  5. div套div 里面div有浮动 外面div自适应高度

    <div style="background-color:red;"> <div style="float:left;background-color: ...

  6. Openjudge-NOI题库-Pell数列

    题目描述 Description Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > ...

  7. MediaPlayer的错误列表速查(android)

    public static final int MEDIA_ERROR_IO Added in API level 17 File or network related operation error ...

  8. ngrok内网穿透神器

    ngrok类似国内的花生壳,可以将本地的内网映射到公网上,这样就可以做web开发,微信开发了.下面就介绍下ngrok是怎么配置的吧. 方式一: 一.打开ngrok的官网https://ngrok.co ...

  9. javaMail邮件发送的简单实现

    package com.test.mail; import java.util.Properties; import javax.mail.Message; import javax.mail.Ses ...

  10. 【Python之路】第六篇--Python基础之模块

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...