Bone Collector II(链接)
Sample Input
3 5 10 2 1 2 3 4 5 5 4 3 2 1 5 10 12 1 2 3 4 5 5 4 3 2 1 5 10 16 1 2 3 4 5 5 4 3 2 1
Sample Output
12 2 0
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int value[maxn];
int weight[maxn];
int dp[maxn][maxn];
int main()
{
int T,N,V,K;
scanf("%d",&T);
while(T--)
{
int a[maxn],b[maxn],p,x,y,z;
memset(dp,0,sizeof(dp)); //注意赋初值
scanf("%d%d%d",&N,&V,&K);
for(int i=0;i<N;i++) scanf("%d",&value[i]);
for(int i=0;i<N;i++) scanf("%d",&weight[i]);
for(int i=0;i<N;i++)
{
for(int j=V;j>=weight[i];j--)
{
for(p=1;p<=K;p++)
{
a[p]=dp[j-weight[i]][p]+value[i];
b[p]=dp[j][p];
}
a[p]=b[p]=-1;
x=y=z=1;
while(z<=K&&(a[x]!=-1||b[y]!=-1))
{
if(a[x]>b[y])
{
dp[j][z]=a[x];
x++;
}
else
{
dp[j][z]=b[y];
y++;
}
if(dp[j][z]!=dp[j][z-1])
z++;
}
}
}
printf("%d\n",dp[V][K]);
}
return 0;
}