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. Unity3DGUI:鼠标click

    Input函数监测鼠标操作 鼠标点击事件 鼠标双击事件

  2. 搭建Gitlab

    这个周末好奇想搭建一个私有的Gitlab,以前折腾过一次.现在记录如下. 一.安装Ubuntu 16.04 LTS server amd64 到Virtual Box 二.安装gitlab服务器 按照 ...

  3. moodle笔记之-权限api

    <?php//权限定义$capabilities = array( 'mod/mytest:managefiles' => array(//具体的权限:插件类型/插件名/权限 这里是增加一 ...

  4. Java获取IP

    public static String getIpAddr(HttpServletRequest request) {        String ip = request.getHeader(&q ...

  5. Knockout js 绑定 radio 时,当绑定整形的时候,绑定不生效

    解决方案: 使用checkedValue和checked 组合,如下代码. <div><input type="radio" name="flavorG ...

  6. C#中使用like和in参数传值

    like 参数string strSql = "select * from Person.Address where City like '%'+ @add + '%'";SqlP ...

  7. Codeforces Round #346 (Div. 2) B Qualifying Contest

    B. Qualifying Contest 题目链接http://codeforces.com/contest/659/problem/B Description Very soon Berland ...

  8. Python编程工具IDLE快捷键

    IDLE编辑器快捷键 自动补全代码        Alt+/(查找编辑器内已经写过的代码来补全) 补全提示              Ctrl+Shift+space(默认与输入法冲突,修改之) (方 ...

  9. sublime text 安装 SFTP

    1  先安装Package Control import urllib.request,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7c0f' + '1e ...

  10. 《JavaScript高级程序设计》读书笔记 ---创建对象

    虽然Object 构造函数或对象字面量都可以用来创建单个对象,但这些方式有个明显的缺点:使用同一个接口创建很多对象,会产生大量的重复代码.为解决这个问题,人们开始使用工厂模式的一种变体. 工厂模式工厂 ...