HDU

I love sneakers!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 367 Accepted Submission(s): 131
 
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

这个题大意是有n种商品,一共有k个品牌,现在有m元,要求每个品牌的商品最少买一个,问最多能买多少?
这个问题其实类似于分组背包。
因为每个品牌的商品都要买到,所以一但有一个商品买不到就是 “impossible”,因此,我们可以让每一种状态的初始为负无穷。
从第一个品牌开始找,先找出第一种品牌的各种成本对应的最大利润
然后看下一个品牌,对应当前状态,要么找该个同品牌的商品加入到之前能达到的某个状态里,即 a[i][k]=max(a[i][k],a[i][k-s[j].w]+s[j].v);
要么把该品牌的这个物品加入到上一个品牌之后,看2者组合是或能达到一个新的最大状态,即a[i][k]=max(a[i][k],a[i-1][k-s[j].w]+s[j].v);
这样,最后只需要坚持 a[k][m]就可以了,因为如果有一个品牌空缺,那么该品牌对应的所有状态都为初始状态 即负无穷。因为之后的状态都建立在负无穷
的基础上的,因为无论如何都小于0,这样就很容易判断了。

 

 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct T
{
    int b,w,v;
};
T s[1005];
int a[105][10005];
int main()
{
    int n,m,k;
    while(cin>>n>>m>>k)
    {
        for(int i=0;i<n;i++)
            cin>>s[i].b>>s[i].w>>s[i].v;
        memset(a,0,sizeof(a));
        for(int i=1;i<=k;i++)
            for(int j=0;j<=m;j++)
                a[i][j]=-INT_MIN;
        for(int i=1;i<=k;i++)
            for(int j=0;j<n;j++)
                for(int k=m;k>=0;k--)
                    if(i==s[j].b && k-s[j].w>=0)
                    {
                        a[i][k]=max(a[i][k],a[i][k-s[j].w]+s[j].v);
                        a[i][k]=max(a[i][k],a[i-1][k-s[j].w]+s[j].v);
                    }
        if(a[k][m]<0) cout<<"Impossible"<<endl;
        else
            cout<<a[k][m]<<endl;
    }
    return 0;
}

 

posted @ 2012-11-03 00:37  钟迪  阅读(448)  评论(0)    收藏  举报