hdu1114-E - Piggy-Bank(完全背包)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<memory.h>
#define Maxn 10050
#define INF 0xffffff
int val[550],weight[550];
int dp[Maxn];
typedef long long ll;
using namespace std;
int main()
{   int T,E,F,N,sum;
    scanf("%d",&T);
    while(T--)
    {   //memset(dp,INF,sizeof(dp));
        scanf("%d%d",&E,&F);sum=F-E;
        scanf("%d",&N);
        for(int i=1;i<=sum;i++)
            dp[i]=INF;
            dp[0]=0;
        for(int i=0;i<N;i++)
            scanf("%d%d",&val[i],&weight[i]);
        for(int i=0;i<N;i++)
            for(int j=weight[i];j<=sum;j++)
            dp[j]=min(dp[j],dp[j-weight[i]]+val[i]);
        if(dp[sum]==INF){printf("This is impossible.\n");}
        else {printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sum]);}
    }
    return 0;
}


你可能感兴趣的:(hdu1114-E - Piggy-Bank(完全背包))