dp之多重背包hdu1114

题目很水,不多说.........

#include<stdio.h>

int main()

{

    long t,n,m,a,i,j,dp[10005],vol[505],jizhi[505],sum,w;

    scanf("%ld",&t);

    while(t--)

    {

        w=0;

        scanf("%ld%ld",&n,&m);

        sum=m-n;

        scanf("%ld",&a);

        for(i=0;i<a;i++)

        {

            scanf("%ld%ld",&jizhi[i],&vol[i]);

            if(vol[i]<=sum)

                w=1;

        }

        if(w==0)

            printf("This is impossible.\n");

        else

        {

            for(i=0;i<10005;i++)

                dp[i]=10000000;

            dp[0]=0;

            for(i=0;i<a;i++)

                for(j=vol[i];j<=sum;j++)

                    if(dp[j]>dp[j-vol[i]]+jizhi[i])

                        dp[j]=dp[j-vol[i]]+jizhi[i];

            if(dp[sum]<10000000)

            printf("The minimum amount of money in the piggy-bank is %ld.\n",dp[sum]);

            else

                printf("This is impossible.\n");



        }

    }

    return 0;

}

 

你可能感兴趣的:(HDU)