HDU1114 Piggy-Bank

完全背包~~

#include <iostream>

using namespace std;

int main()
{
    int cas,w1,w2,dp[10001],vol[501],val[501],i,j,n;
    cin>>cas;
    while(cas --)
    {
        cin>>w1>>w2>>n;
        for(i = 0;i < n;i ++)
        cin>>val[i]>>vol[i];
        for(i = 0;i < 10001;i ++) dp[i] = 1000000;
        dp[0] = 0;
        for(i = 0;i < n;i ++)
        for(j = vol[i];j <= w2-w1;j ++)
        dp[j] = min(dp[j],dp[j-vol[i]] + val[i]);
        if(dp[w2-w1] == 1000000) cout<<"This is impossible."<<endl;
        else cout<<"The minimum amount of money in the piggy-bank is "<<dp[w2-w1]<<"."<<endl;
    }
    return 0;
}


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