HDU 3033 I love sneakers! (DP 01背包+完全背包)

来源:互联网 发布:达芬奇调色 知乎 编辑:程序博客网 时间:2024/06/06 00:10
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 31 4 62 5 73 4 991 55 772 44 66
 


Sample Output
255


题意:k个品牌的鞋子,每个牌子里至少选一个,且每个牌子都要选,问价值最大。

如果只看牌子,那么就是01背包,对于每个牌子,又是像是一个完全背包。

dp[i][j]代表前i个品牌花费为j时产生的最大价值。

一开始只有dp[0]这一行为0,其他dp值都设为-1,因为没有牌子价值为0是合法的。

dp[i][j] = Max( dp[i-1][j-w[k]]+v[k] , dp[i][j-w[k]]+v[k] ,dp[i][j] ) , 这个式子代表了第一次选i这个品牌,和已经选过i品牌再次选择其中的鞋子,和自己本身 三者取最大值。

#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;typedef long long LL;const int MAX=0x3f3f3f3f;int n,m,k,num[105],v[105],w[105],dp[105][10005];int Max(int a,int b,int c) {    return max( max(a,b) , c );}int main(){    while(~scanf("%d%d%d",&n,&m,&k)) {        memset(dp,-1,sizeof(dp));        memset(dp[0],0,sizeof(dp[0]));        for(int i=1;i<=n;i++) scanf("%d%d%d",&num[i],&w[i],&v[i]);        for(int i=1;i<=k;i++)            for(int j=1;j<=n;j++)                if( num[j] == i ) for(int p=m;p >= w[j];p--)                    dp[i][p] = Max( dp[i][p] ,dp[i-1][ p-w[j] ]+v[j] ,dp[i][ p-w[j] ]+v[j] );        if(dp[k][m] < 0) printf("Impossible\n");        else printf("%d\n",dp[k][m]);    }    return 0;}



0 0