加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

hdu 3033 I love sneakers!

发布时间:2020-12-15 08:22:38 所属栏目:Java 来源:网络整理
导读:I love sneakers! Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8105????Accepted Submission(s): 3216 Problem Description After months of hard working,Iserlohn finally wins awesome a

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8105????Accepted Submission(s): 3216

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

题意:有k种鞋子一共N双,每双鞋子具有a-种类,b-费用,c-价值,每种类型的鞋子至少选一双,求花M元获得的最大价值。

?

题解:分组背包变形题。令dp[k][j]表示为前 k 组花费为 j 时的最大价值,因为每种类型的鞋子至少选一双,所以对每组进行01背包,当状态转移时,为了保证前(k-1)组每组至少选一双鞋子,dp[][]初始化时需要特殊处理。

?

代码:

#include<iostream> #include<algorithm> #include<cstdio> #include<vector>
using namespace std; struct node { int w,v; node(){} node(int a,int b){w=a;v=b;} }; vector<node>type[15]; int dp[11][10005]; int main() { int i,j,k,N,M,K,a,b,c; while(~scanf("%d%d%d",&N,&M,&K)) { { for(i=0;i<15;i++) type[i].clear(); fill(dp[0],dp[0]+11*10005,-1); //精髓 fill(dp[0],dp[0]+10005,0); } for(i=1;i<=N;i++) { scanf("%d%d%d",&a,&b,&c); type[a].push_back(node(b,c)); } for(k=1;k<=K;k++) for(i=0;i<type[k].size();i++) for(j=M;j>=type[k][i].w;j--) dp[k][j]=max(dp[k][j],max(dp[k-1][j-type[k][i].w]+type[k][i].v,dp[k][j-type[k][i].w]+type[k][i].v)); if(dp[K][M]!=-1) printf("%dn",dp[K][M]); else printf("Impossiblen"); } return 0; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读