hdu 3033 I love sneakers!(分组背包)

来源:互联网 发布:平面动画制作软件 编辑:程序博客网 时间:2024/05/17 03:11

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 31 4 62 5 73 4 991 55 772 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 0
原创粉丝点击