#include<iostream>
using namespace std;
#define min(x,y) ((x)<(y)?(x):(y))
#define MAX_INT 10000000
int p[501];
int w[501];
int f[10001];
int main()
{
freopen("knap_input2.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
int i,j,E,F,n,W;
for(i=0;i<10001;++i)
{
f[i] = MAX_INT; //初始化很重要
}
f[0] = 0;
cin>>E;
cin>>F;
cin>>n;
W=F-E;
cout<<"n="<<n<<",W="<<W<<endl;
for(i=0;i<n;++i)
{
cin>>p[i];
cin>>w[i];
}
for(i=0;i<n;i++)
{
for(j=w[i];j<=W;++j)
{
cout<<"j="<<j<<",d[i]="<<p[i]<<endl;
cout<<"min("<<f[j]<<","<<f[j-w[i]]+p[i]<<")"<<endl;
f[j] = min(f[j],f[j-w[i]]+p[i]);
cout<<"f["<<j<<"]="<<f[j]<<endl;
}
for(int k=0;k<=110;k++)
{
cout<<f[k]<<" ";
}
cout<<endl;
}
if(f[W] != MAX_INT)
{
cout<<"The minumum amount of money in the piggy-bank is "<<f[W]<<"."<<endl;
} else {
cout<<"This is impossible.\n";
}
}
return 0;
}
背包2
最新推荐文章于 2022-04-06 15:49:57 发布