输出路径,搞了一个DFS出来,主要是这里,浪费了好长时间。
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[][];
int c[],v[],que[];
int maxz,ti;
void dfs(int n,int T,int step)
{
int i,t1,t2;
if(n == )
{
if(maxz < step)
{
maxz = step;
sort(que,que+step);
t1 = t2 = ;
for(i = ;i < step;i ++)
{
t1 += que[i];
t2 += t1;
}
ti = t2;
}
else if(maxz == step)
{
sort(que,que+step);
t1 = t2 = ;
for(i = ;i < step;i ++)
{
t1 += que[i];
t2 += t1;
}
ti = min(t2,ti);
}
return ;
}
if(dp[n][T] == dp[n-][T])
dfs(n-,T,step);
if(T >= c[n]&&dp[n][T] == dp[n-][T-c[n]] + v[n])
{
que[step] = c[n];
dfs(n-,T-c[n],step+);
}
return ;
}
int main()
{
int cas,T,n,i,j;
scanf("%d",&cas);
while(cas--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&T,&n);
for(i = ;i <= n;i ++)
scanf("%d",&c[i]);
for(i = ;i <= n;i ++)
scanf("%d",&v[i]);
for(i = ;i <= n;i ++)
{
for(j = ;j <= T;j ++)
{
if(j >= c[i])
dp[i][j] = max(dp[i-][j],dp[i-][j-c[i]]+v[i]);
else
dp[i][j] = dp[i-][j];
}
}
maxz = ;
dfs(n,T,);
printf("%d %d %d\n",dp[n][T],maxz,ti);
}
return ;
}