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. 让webstorm支持新建.vue文件

    1. 首先安装vue插件,安装方法: file-->setting  -->  plugin  ,点击plugin,在内容部分的左侧输入框不用输入任何东西,直接点击下图中的按钮. 如下图所 ...

  2. CentOS 6.5安装PostgreSQL9.3.5时报错: jade: Command not found

    CentOS 6.5安装PostgreSQL9.3.5时报错: jade: Command not found 1[root@pghost1 postgresql-9.3.5]# ./configur ...

  3. Python初识与简介【开篇】

    目录 1.扯淡 2.Python历史 3.Python简介 4.Python应用 5.为什么是python而不是其他语言? 6.Python的种类 7.Python的特点 8.Python设计哲学 9 ...

  4. sharepoint:基于AD的FORM认证

    //来源:http://www.cnblogs.com/jindahao/archive/2012/05/07/2487351.html 需求: 1. 认证要基于AD 2. 登入方式要页面的方式(fo ...

  5. Docker 总结

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+] Docker总结 简单介绍 1 Docker 架构 安装和环境配置 1 mac 11 brew安装 11 dmg文件安装 1 ...

  6. Select In SQL Server-Cross Instance in same domain and different domain

    Same Domain: Exec sp_addlinkedserver 'PC087':Add Remote Server Exec sp_dropserver 'InstcanceName':De ...

  7. mysql最大连接数问题

    进入mysql系统就, 查询最大连接数:show variables like 'max_connections'; 修改最大连接数:set global max_connections=1000;

  8. 101个Linq例子(40-60)

    GroupBy - Simple 2 public void Linq41() { string[] words = { "blueberry", "chimpanzee ...

  9. hibernate增删改查

    -----------增加--------- public void insertUsers(String userName,String userPwd) { Users u=new Users() ...

  10. zTree 勾选checkbox

    zTree 勾选checkbox var setting = {    check: {        enable: true//        chkboxType : { "Y&quo ...