题意:
给定n个物品 m元,k种商品
下面n行 tpye w v 表示物品的牌子,价格,价值(每个物品只有1件)
问:每个牌子至少买一件能获得的最大价值是多少
思路:分组背包
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define ll int
ll n, m, k;
ll dp[11][10050]; //dp[i][j] 前i组花了j元的最大价值
struct node{
ll w,v;
}p[11][101];
ll top[11];
int main(){
ll i, j, u, v, type;
while(~scanf("%d %d %d",&n,&m,&k)){
memset(dp, -1, sizeof dp);
memset(top, 0, sizeof top);
for(i=1;i<=n;i++){
scanf("%d %d %d",&type,&u,&v);
node E = {u,v};
p[type][top[type]++] = E;
}
memset(dp[0], 0, sizeof dp[0]);
for(i=1;i<=k;i++)
{
for(j = 0; j < top[i]; j++)
{
node now = p[i][j];
for(ll z = m; z >= 0; z--)
{
/*
if(dp[i-1][z]!=-1 && z+now.w <= m)
dp[i][z+now.w] = max(dp[i][z+now.w], dp[i-1][z]+now.v);
if(dp[i][z]!=-1 && z+now.w <= m)
dp[i][z+now.w] = max(dp[i][z+now.w], dp[i][z]+now.v); */
if(z-now.w >= 0 && dp[i][z-now.w]!=-1)
dp[i][z] = max(dp[i][z], dp[i][z-now.w]+now.v);
if(z-now.w >= 0 && dp[i-1][z-now.w]!=-1)
dp[i][z] = max(dp[i][z], dp[i-1][z-now.w]+now.v);
}
}
}
ll ans = dp[k][m];
if(ans==-1)puts("Impossible");
else
printf("%d\n",ans);
}
return 0;
}