题目;
代码;
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; struct beibao{ int val; int vol; }bag[1010]; int main() { int dp[1010]; int i,j,n,v; int t; cin>>t; while(t--) { cin>>n>>v; for(i=1; i<=n; i++) cin>>bag[i].val; for(i=1; i<=n; i++) cin>>bag[i].vol; memset(dp,0,sizeof(dp)); for(i=1; i<=n; i++) for(j=v; j>=bag[i].vol; j--) dp[j]=max(dp[j],dp[j-bag[i].vol]+bag[i].val); cout<<dp[v]<<endl; } return 0; }
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; struct beibao{ int val; int vol; }bag[1010]; int main() { //freopen("input.txt","r",stdin); int dp[1010]; int i,j,n,v; int t; int sum,bound; cin>>t; while(t--) { cin>>n>>v; for(i=1; i<=n; i++) cin>>bag[i].val; for(i=1; i<=n; i++) cin>>bag[i].vol; memset(dp,0,sizeof(dp)); for(i=1; i<=n; i++) { sum = 0; for(j=i; j<=n; j++) sum += bag[j].val; bound = max(v-sum,bag[i].vol); for(j=v; j>=bound; j--) dp[j]=max(dp[j],dp[j-bag[i].vol]+bag[i].val); } cout<<dp[v]<<endl; } return 0; }
hdu 2602 Bone Collector,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u013147615/article/details/25914277