#include<iostream>
using namespace std;
#define max(x,y) ((x)>(y)?(x):(y))
int w[3403];
int d[3403];
int f[13000];
int main()
{
int i,j,n,m;
int nMax = 0;
freopen("knapsack_input.txt","r",stdin);
cin>>n;
cin>>m;
cout<<"n="<<n<<",m="<<m<<endl;
for(i=1;i<=n;i++)
{
cin>>w[i];
cin>>d[i];
}
cout<<"weight[i]:"<<endl;
for(i=1;i<=n;i++)
{
cout<<w[i]<<" ";
}
cout<<endl;
cout<<"desirability[i]:"<<endl;
for(i=1;i<=n;i++)
{
cout<<d[i]<<" ";
}
cout<<endl;
for(i=1;i<=n;++i)
{
cout<<"i="<<i<<",m("<<m<<") > w["<<i<<"]="<<w[i]<<endl;
for(j=m;j>=w[i];--j)
{
cout<<"j="<<j<<",d[i]="<<d[i]<<endl;
cout<<"max("<<f[j]<<","<<f[j-w[i]]+d[i]<<")"<<endl;
f[j] = max(f[j],f[j-w[i]]+d[i]);
cout<<"f["<<j<<"]="<<f[j]<<endl;
}
for(int k=0;k<=50;k++)
{
cout<<f[k]<<" ";
}
cout<<endl;
}
cout<<f[m]<<endl;
return 0;
}
背包(1)
最新推荐文章于 2024-05-12 18:32:08 发布