Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。 请问:你用有限的资金最多能采购多少公斤粮食呢? Input 输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类,然后是m行数据,每行包含3个数p,h和c(1<=p<=20,1<=h<=200,1<=c<=20),分别表示每袋的价格、每袋的重量以及对应种类大米的袋数。
Output 对于每组测试数据,请输出能够购买大米的最多重量,你可以假设经费买不光所有的大米,并且经费你可以不用完。每个实例的输出占一行。
Sample Input 1 8 2 2 100 4 4 100 2
Sample Output 400
思路:其实思路并不是很难,就是把多重背包装换成01背包问题。思路看代码:
#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; struct E{ int w; int v; }list[101]; int dp[101]; int main() { int T; scanf("%d",&T); while(T--){ int s,n; scanf("%d%d",&s,&n); int cnt=0; for(int i=1;i<=n;i++) { int v,w,k; scanf("%d%d%d",&w,&v,&k); int c=1; while(k-c>0)//对数字拆分,拆成1,2,4.。。,这样就可以把其转换成01背包问题了 { k-=c; list[++cnt].v=c*v; list[cnt].w=c*w; c*=2; } list[++cnt].w=k*w; list[cnt].v=k*v; } for(int i=1;i<=s;i++) dp[i]=0; for(int i=1;i<=cnt;i++){ for(int j=s;j>=list[i].w;j--){ dp[j]=max(dp[j],dp[j-list[i].w]+list[i].v); } } printf("%d\n",dp[s]); } system("pause"); return 0; }