题目:能否最小价值的钱来装满整个储钱罐,每个放入储钱罐的钱的数量不限.完全背包问题
思路:------
dp,从0开始
AC program:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iomanip>
using namespace std;
int value[505],weight[505];
int dp[10005];
#define inf 1000000000
int main()
{
int test,n,v;
int pWeight,amountW;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&pWeight,&amountW);
v=amountW-pWeight;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&value[i],&weight[i]);
dp[0]=0;//!!!
for(int i=1;i<=v;i++)
dp[i]=inf;
for(int i=1;i<=n;i++)
for(int j=0;j<=v;j++)
{
if(j>=weight[i])
dp[j]=min(dp[j],dp[j-weight[i]]+value[i]);
}
if(dp[v]<inf)
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]);
else
printf("This is impossible.\n");
}
//system("pause");
return 0;}
0-1逆序,完全顺序..orz,,,