微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

hdu 3033 I love sneakers!

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("%d\n",dp[K][M]); else printf("Impossible\n"); } return 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐