hdu3033(分组背包,数据纯属恶心)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=3033

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3290    Accepted Submission(s): 1352


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
 

题意:A君要买运动鞋,运动鞋一共有n种,分属k种品牌。每个品牌至少买一种,求最高收益。(如果钱不够每个品牌买一种鞋,输出“Impossible”)

思路:分组背包。对于品牌为n的鞋,要判断n和n-1两次,代表是买了多双品牌为n的鞋以及只买了一双品牌为n的鞋。最后根据dp[n][1...v]中的最大值来输出答案。(需要注意,数据比较恶心)

代码:

#include<iostream>
//#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
//#define M 11111
int dp[11][10010],vol[11][110],val[11][110];
int main()
{
    int n,v,k,i,j,m,g;
    while(scanf("%d%d%d",&n,&v,&k)>0)
    {
        g=-1;
        memset(dp,-1,sizeof(dp));
        memset(vol,0,sizeof(vol));
        memset(val,0,sizeof(val));
        for(i=1;i<=n;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            vol[a][vol[a][0]+1]=b;vol[a][0]++;
            val[a][val[a][0]+1]=c;val[a][0]++;
        }
        for(i=1;i<=vol[1][0];i++)  //对于品牌1特殊处理
        {
            for(j=v;j>=vol[1][i];j--)
            {
                if(j==vol[1][i]&&dp[1][j]<val[1][i]) dp[1][j]=val[1][i];  //主要是这一步特殊处理,因为我把dp数组初始值全更新为-1
                else if(dp[1][j-vol[1][i]]!=-1&&dp[1][j]<dp[1][j-vol[1][i]]+val[1][i]) dp[1][j]=dp[1][j-vol[1][i]]+val[1][i];
            }
        }
        for(m=2;m<=k;m++)  //对于比1大的品牌
        {
            for(i=1;i<=vol[m][0];i++)
            {
                for(j=v;j>=vol[m][i];j--)
                {
                    if(dp[m][j-vol[m][i]]!=-1) dp[m][j]=max(dp[m][j-vol[m][i]]+val[m][i],dp[m][j]);  //对比本身
                    if(dp[m-1][j-vol[m][i]]!=-1) dp[m][j]=max(dp[m-1][j-vol[m][i]]+val[m][i],dp[m][j]);  //对比之前
                }  //本身要在之前前面,不然一双鞋可能会买两次
            }
        }
        for(i=v;i>=0;i--)
        {
            if(g<dp[k][i]) g=dp[k][i];  //这里之前没想到,错了3次。。。
        }
        if(g>=0) printf("%d\n",g);
        else puts("Impossible");
    }
    return 0;
}
/*
3 5 3
1 0 5
2 0 1
3 0 2

3 5 3
1 0 0
2 0 0
3 0 0

4 5 2
1 2 3
1 3 2
2 2 3
2 3 2
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值