慶功會
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 5
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
接下來n行,每行3個數,v、w、s,分別表示第I種獎品的價格、價值(價格與價值是不同的概念)和購買的數量(買0件到s件均可),其中v<=100,w<=1000,s<=10。
Output
Sample Input
1 5 1000 80 20 4 40 50 9 30 50 7 40 30 6 20 20 1
Sample Output
1040
思路分析:多重揹包。
代碼:#include<iostream> #include<stdio.h> using namespace std; struct Rice { int bag,price,weight; } rice[7100]; int dp[7100]; int main() { int n,m,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%d%d%d",&rice[i].price,&rice[i].weight,&rice[i].bag); for(int i=0; i<=m; i++) dp[i]=0; for(int i=1; i<=n; i++) { for(int j=m; j>=rice[i].price; j--) for(int k=1; k<=rice[i].bag; k++) { if(j-k*rice[i].price>=0&&dp[j]<dp[j-k*rice[i].price]+k*rice[i].weight) dp[j]=dp[j-k*rice[i].price]+k*rice[i].weight; } } cout<<dp[m]<<endl; } return 0; }