hdu 1114 Piggy-Bank dp

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define inf 10000000
int dp[11000];
int n;
int val[550],wei[550];
int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas--)
    {
        int w1,w2,w;
        int i,j;
        scanf("%d%d",&w1,&w2);
        w=w2-w1;
        for(i=0;i<=w;i++) dp[i]=inf;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d%d",&val[i],&wei[i]);
        dp[0]=0;
        for(i=1;i<=n;i++)
            for(j=wei[i];j<=w;j++)
                dp[j]=min(dp[j],dp[j-wei[i]]+val[i]);
        if(dp[w]==inf) printf("This is impossible.\n");
        else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[w]);
    }
    return 0;
}
posted @ 2014-07-16 10:32  贝尔摩德  阅读(149)  评论(0编辑  收藏  举报