标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42179 Accepted Submission(s):
17543
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int max(int a,int b) 6 { 7 return a>b?a:b; 8 } 9 int main() 10 { 11 int T,n,m,i,j; 12 int a[1005],b[1005]; 13 scanf("%d",&T); 14 while(T--) 15 { 16 scanf("%d%d",&n,&m); 17 for(i=0; i<n; i++) //输入价值 18 scanf("%d",&a[i]); 19 for(i=0; i<n; i++) //输入体积 20 scanf("%d",&b[i]); 21 int dp[1005]; //dp数组始终记录当前体积的最大价值 22 memset(dp,0,sizeof(dp)); //dp全部初始化为0 23 for(i=0; i<n; i++) //从第一个开始循环 24 for(j=m; j>=b[i]; j--) 25 dp[j]=max(dp[j],dp[j-b[i]]+a[i]); //比较放入i物体后的价值与不放之前的价值,记录大的值 26 printf("%d\n",dp[m]); //输入总体积的最大价值 27 } 28 return 0; 29 }
标签:
原文地址:http://www.cnblogs.com/pshw/p/5003341.html