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; }

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...