POJ - 1384 Piggy-Bank

算法:裸的完全背包

难度:NOIP-

代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
#define N 505
using namespace std;
struct node
{
	int w;
	int hhf;
}a[N];
int dp[10005];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		int acc=m-n;
		int zz;
		scanf("%d",&zz);
		for(int i = 1;i <= zz;i++)
		{
			scanf("%d%d",&a[i].w,&a[i].hhf);
		}
		memset(dp,0x3f3f3f3f,sizeof(dp));
		dp[0]=0;
		for(int i = 1;i <= zz;i++)
		{
			for(int j = a[i].hhf;j <= acc;j++)
			{
				dp[j]=min(dp[j-a[i].hhf]+a[i].w,dp[j]);
			}
		}
		if(dp[acc]==0x3f3f3f3f) puts("This is impossible.");
		else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[acc]);
	}
	return 0 ;
} 

 

你可能感兴趣的:(动态规划-背包DP)