Hangdian 3033---a group backpack with a little difficulty. .

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

Understand the meaning of the question by yourself:

The main idea is to make some changes on the 01 backpack, and it should be noted that the initialization of the dp array will be different. This requires attention, which is very important. . (Actually, I didn't rely on my personal ability to solve this problem. After reading the code of Shenniu, I got an insight...)

Okay, see the code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=105;
int dp[15][100000];
int n,m,k;
int a[maxn],b[maxn],c[maxn];
int max(int a,int b,int c)
{
   int s[3];
   s[0]=a;
   s[1]=b;
   s[2]=c;
   sort(s,s+3);
   return s[2];
}
int main()
{
    int i,j,t;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {

        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
        }
       // memset(dp)
       for(i=0;i<m;i++)
            dp[0][i]=0;//初始化,,
        for(i=1;i<=k;i++)
        {
            for(j=0;j<=m;j++)
            {
                dp[i][j]=-100000;//与 一般的初始化有点不同,,注意。。
            }
        }
        for(i=1;i<=k;i++)
        {
            for(j=1;j<=n;j++)
            if(a[j]==i)//这里一般比较会容易错,,注意。。
            {
                for(t=m;t>=b[j];t--)
                {
                    dp[i][t]=max(dp[i][t],dp[i][t-b[j]]+c[j],dp[i-1][t-b[j]]+c[j]);//z这里就是多个比较,比较多个,选出最大的。
                }
            }
        }
       // int ans=max(2,1,7);
       // cout<<ans<<"###"<<endl;
        if(dp[k][m]>=0)
            printf("%d\n",dp[k][m]);
        else
            printf("Impossible\n");
    }
    return 0;

}


 

Guess you like

Origin blog.csdn.net/u010200793/article/details/11658481