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

来源:互联网 发布:软件二次开发申请专利 编辑:程序博客网 时间:2024/06/06 10:51

I love sneakers!

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


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
 


n双鞋 m元 有k款鞋

在不超过总钱数的前提下  每款鞋至少要买一双   求最大值 如果有一款鞋买不到 就是Impossible

首先 我们要知道如何判断 哪几款球鞋是买不到的 所以要将dp初始化为-1  与0进行区分

进行状态转移的时候  前一状态必须是可达的 

另外要注意两个状态转移方程的顺序。。。

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)#define Max(a,b) a>b?a:busing namespace std;struct node{    int v,w;}no[20][110];int dp[20][10010];int num[20];int main(){//    fread;    int n,m,k;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        MEM(num,0);        for(int i=0;i<n;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            no[a][num[a]].w=b;            no[a][num[a]].v=c;            num[a]++;        }        MEM(dp,-1); MEM(dp[0],0);        for(int i=1;i<=k;i++)        {            for(int j=0;j<num[i];j++)            {                for(int l=m;l>=no[i][j].w;l--)                {                    if(dp[i][l-no[i][j].w]!=-1) dp[i][l]=Max(dp[i][l],dp[i][l-no[i][j].w]+no[i][j].v);                    if(dp[i-1][l-no[i][j].w]!=-1) dp[i][l]=Max(dp[i][l],dp[i-1][l-no[i][j].w]+no[i][j].v);                    //注意这两个的顺序问题。。。WA哭了//                    if(dp[i][l-no[i][j].w]!=-1) dp[i][l]=Max(dp[i][l],dp[i][l-no[i][j].w]+no[i][j].v);                }            }        }        if(dp[k][m]<0) puts("Impossible");        else printf("%d\n",dp[k][m]);    }    return 0;}


 





0 0
原创粉丝点击