POJ 1384 Piggy-Bank

POJ 1384 Piggy-Bank_第1张图片

 

POJ 1384 Piggy-Bank_第2张图片

 

 代码:

#include
#include
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f;
int main(){
    int t;
    cin>>t;
    while(t--){
        int E,F;
        scanf("%d%d",&E,&F);
        int n;
        scanf("%d",&n);
        int p[n+1],w[n+1];
        for(int i=1;i<=n;i++){
            scanf("%d%d",&p[i],&w[i]);
        }
        ll dp[F+1];
        for(int i=1;i<=F;i++)
            dp[i] = inf; 
        dp[0]=0;
        for(int i=1;i<=n;i++){
            for(int j = w[i];j<=(F-E);j++){
                dp[j] = min(dp[j],dp[j-w[i]]+p[i]);
            }
        }
        if(dp[F-E]==inf)
            cout<<"This is impossible."<<endl;
        else{
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[F-E]);
        }
    }
    return 0;
}

 

你可能感兴趣的:(POJ 1384 Piggy-Bank)