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): 3917    Accepted Submission(s): 1614


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
 


每类物品只能选一个,在花费不超过m的情况下,使价值最大,关键在每类内最少选一个。

dp[i][0]初值为0,其他的为-1,为了当价值全为0时,判断是否有可能每类都取一个。

每个物品只能有三种可能:

1,dp[i][k]在i类物品中不选当前物品。

2,dp[i-1][k-v[j]]+w[j]是选择当前物品,但是是第一次在本组中选,由于开始将该组dp赋为了-1,所以第一次取时,必须由上一组的结果推知,这样才能保证得到全局最优解;

3,dp[i][k-v[j]]+w[j]表示选择当前物品,并且不是第一次取。

状态转移方程:

if(dp[i][k-v[i]]!=-1)

dp[i][k]=max(dp[i][k] , dp[i][k - v[i]]+ w[i] )

if(dp[i-1][k-v[i]]!=-1)

dp[i][k]=max(dp[i][k] , dp[i-1][k-v[i]] + w[i] )

代码:

//46ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000+100;
int dp[12][maxn];
struct node
{
    int a;
    int c;
    int v;
}edge[105];
int max(int a,int b,int c)
{
    int t=a>b?a:b;
    return t>c?t:c;
}
bool cmp(node x,node y)
{
    return x.a<y.a;
}
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=1;i<=n;i++)
        scanf("%d%d%d",&edge[i].a,&edge[i].c,&edge[i].v);
        memset(dp,-1,sizeof(dp));
        memset(dp[0],0,sizeof(dp[0]));
        sort(edge+1,edge+n+1,cmp);
        for(int i=1;i<=n;i++)
        {
            int r=edge[i].a;
            for(int v=m;v>=edge[i].c;v--)
            {
                if(dp[r][v-edge[i].c]!=-1)
                dp[r][v]=max(dp[r][v],dp[r][v-edge[i].c]+edge[i].v);
                if(dp[r-1][v-edge[i].c]!=-1)
                dp[r][v]=max(dp[r][v],dp[r-1][v-edge[i].c]+edge[i].v);
              //注意这里的2个if的顺序,如果上面面这个if在上面的话,当体积为0时,相当于,
              //Max( dp[k][j], dp[k][j] + m[k][i].v); dp[k][j]的值很可能会被计算两次。
            }
        }
        if(dp[k][m]<0)
         printf("Impossible\n");
        else
        printf("%d\n",dp[k][m]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值