HDU 1114 Piggy-Bank

题解:完全背包问题(背包要装满)

#include <cstdio>   

using namespace std;  

int e,b,n;  

int f[10005];  

int p[505],w[505];  

int main(){  

    int t;  

    scanf("%d",&t);  

    while(t--){  

        scanf("%d%d",&e,&b);  

        e=b-e;  

        scanf("%d",&n);  

        for(int i=1;i<=n;i++)scanf("%d%d",&p[i],&w[i]);  

        for(int i=1;i<=e;i++)f[i]=1e9;  

        f[0]=0;  

        for(int i=1;i<=n;i++){  

            for(int j=w[i];j<=e;j++){  

                if(f[j-w[i]]+p[i]<f[j])f[j]=f[j-w[i]]+p[i];        

            }     

        }     

        if(f[e]==1e9)printf("This is impossible.\n");  

        else printf("The minimum amount of money in the piggy-bank is %d.\n",f[e]);  

    }  

  

    return 0;     

}   

 

你可能感兴趣的:(pig)