hdu 3033 I love sneakers! 分组背包

I love sneakers!

                                                                            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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.
hdu 3033 I love sneakers! 分组背包_第1张图片
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
题意:有S款运动鞋,一个n件,总钱数为m,求不超过总钱数且每款鞋子至少买一双的情况下,使价值最大。如果 有一款买不到,就输出“ Impossible"。这个题的关键还是在于初始化,如果我们一开始把dp初始化为0,则当所有鞋子的价值都是0时,我们就无法区分是买不全那几款鞋子还是能买全但最大价值是0;因此,要把S!=0的dp[S][j]初始化为-1,便于区分。
这是一个带分组的01背包问题,与普通分组背包不同的是,这个分组背包关键在于每组至少取1个,而不是最多1个。 
d p[i][k]是不选择当前鞋子;
dp[i-1][k-v[j]]+w[j]是选择当前鞋子,但是是第一次在本组中选,由于开始将该组dp赋为了-1,所以第一次取时,必须由上一组的结果推知,这样才能保证得到全局最优解;
dp[i][k-v[j]]+w[j]表示选择当前鞋子,并且不是第一次取。
参考代码:
/*共有S种款式,每张款式不止一双*/ /*d[i][k]=max(d[i][k] , d[i][k - v[i]]+ w[i] , d[i-1][k-v[i]] + w[i] )        d[i][k] 表示 买了1 至 i 种款式的鞋子,使用的钱是 k 产生的最大的价值是 d[i][k];*/
#include<stdio.h>
#include<string.h>
#define N 10005
int dp[102][N],s[102],v[102],w[102];
int max(int a,int b,int c)
{
    int t=a>b?a:b;
    return t>c?t:c;
}
int main()
{
    int n,m,k,S,i,j;
    while(scanf("%d%d%d",&n,&m,&S)!=EOF)
    {
        for(i=0;i<n;i++)
         scanf("%d%d%d",&s[i],&v[i],&w[i]);
        for(i=0;i<=S;i++)
          for(j=0;j<=m;j++)
          {
            if(i==0)
               dp[i][j]=0;
            else
                dp[i][j]=-1;
          }
        for(i=1;i<=S;i++)
          for(j=0;j<n;j++)
            if(s[j]==i)   //第i款
             for(k=m;k>=v[j];k--)
              dp[i][k]=max(dp[i][k],dp[i][k-v[j]]+w[j],dp[i-1][k-v[j]]+w[j]);
             /* 当前的值,没有要这种且没有要这件时的值加上物品价值,要这种不要这件之前的值加上物品价值*/
        if(dp[S][m]<0)
        printf("Impossible\n");
        else
        printf("%d\n",dp[S][m]);
    }
    return 0;
}

你可能感兴趣的:(c,背包)