hdu3033 分组背包

时间:2022-05-01 00:06:20
 //Accepted    896 KB    156 ms
 //http://blog.csdn.net/juststeps/article/details/8712150
 //dp[i][l]=max(dp[i][l],dp[i][l-v[i][j].weight]+v[i][j].value);第i种已经取数后用v[i][j]更新
 //dp[i][l]=max(dp[i][l],dp[i-1][l-v[i][j].weight]+v[i][j].value);第i种还没有取数用v[i][j]更新
 //显然,后一种情况应该后更新
 #include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <queue>
 #include <cmath>
 #include <vector>
 #include <algorithm>
 using namespace std;
 /**
   * This is a documentation comment block
   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
   * @authr songt
   */
 ;
 ;
 struct node
 {
     int weight,value;
 };
 vector<node > vec[imax_n];
 int dp[imax_n][imax_v];
 int n,v;
 int k;
 int max(int a,int b)
 {
     return a>b?a:b;
 }
 void Dp()
 {
     memset(dp,-,sizeof(dp));
     //for (int i=0;i<=k;i++)
     //for (int j=0;j<=v;j++)
     //dp[i][j]=-1;
     ;i<=v;i++)
     {
         dp[][i]=;
     }
     ;i<=k;i++)
     {
         ;j<vec[i].size();j++)
         {
             for (int l=v;l>=vec[i][j].weight;l--)
             {
                 )
                 {
                     dp[i][l]=max(dp[i][l],dp[i][l-vec[i][j].weight]+vec[i][j].value);
                 }
                 ][l-vec[i][j].weight]!=-)
                 {
                     dp[i][l]=max(dp[i][l],dp[i-][l-vec[i][j].weight]+vec[i][j].value);
                 }
             }
         }
     }
     )
     {
         printf("Impossible\n");
     }
     else
     {
         printf("%d\n",dp[k][v]);
     }
 }
 int main()
 {
     while (scanf("%d%d%d",&n,&v,&k)!=EOF)
     {
         int kind;
         node x;
         ;i<=;i++)
         vec[i].clear();
         ;i<=n;i++)
         {
             scanf("%d%d%d",&kind,&x.weight,&x.value);
             vec[kind].push_back(x);
         }
         Dp();
     }
     ;
 }