商店中每个商品都有相应的价值,当一些商品形成组合时,总价格会变低。给定要买的商品及数量,问怎么组合总价最低?
状压DP+完全背包
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int b,c,k,p[110],tot,w[110],s,n,dp[25010],res;
int mp[1005];
int h[10]={0,1,6,36,216,1296,7776,46656,279936};
int main(){
#ifdef WINE
freopen("data.in","r",stdin);
#endif
scanf("%d",&b);
for(int i=1;i<=b;i++){
scanf("%d%d%d",&c,&k,&p[i]);
mp[c]=i;
tot+=k*h[i];
res+=k*p[i];
w[i]=h[i];
}
scanf("%d",&s);
for(int i=1;i<=s;i++){
scanf("%d",&n);
int cw=0;
while(n--){
scanf("%d%d",&c,&k);
int tmp=mp[c];
if(tmp==0)continue;
cw+=k*h[tmp];
}
w[b+i]=cw;
scanf("%d",&p[b+i]);
}
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=1;i<=b+s;i++)
for(int j=w[i];j<=tot;j++)
dp[j]=min(dp[j],dp[j-w[i]]+p[i]);
printf("%d\n",min(dp[tot],res));
return 0;
}