Tanky WooRSS

HDOJ 1114 Piggy-Bank

31 Jul 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

完全背包~~~ 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1114

01背包的题: http://www.wutianqi.com/?p=533

代码: // 初始化为无穷大

// Author: Tanky Woo
// HDOJ 1114


#include 
using namespace std;

int nCases;
int nPack, nVolume1, nVolume2, nVolume;
int weight[510], value[510];
int record[10000];
const int INF = 1000000001;
int main()
{    
    //freopen("input.txt", "r", stdin);

    scanf("%d", &nCases;);
    while(nCases--)
    {
        scanf("%d %d", &nVolume1;, &nVolume2;);
        nVolume = nVolume2-nVolume1;
        scanf("%d", &nPack;);
        for(int i=0; irecord[j-weight[i]]+value[i])
                    record[j] = record[j-weight[i]]+value[i];
        if(record[nVolume] == INF)
            printf("This is impossible.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n",record[nVolume]);

    }
    return 0;
}

// 这是ambition神牛的版本 // 初始化为-1

#include
int num[10001],w[500],v[500];
main()
{
    int n,m,e,f,t,i,j;
    for(scanf("%d",&t;);t>0;t--)
    {
        scanf("%d%d",&e;,&f;);
        m=f-e;
        for(scanf("%d",&n;),i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        num[0]=0;
        for(i=1;i<=m;i++)
            num[i]=-1;
        for(i=0;i<n;i++)
        {
            for(j=w[i];j<=m;j++)
            {
                if(num[j-w[i]]!=-1&&num;[j]!=-1)
                {if(num[j-w[i]]+v[i]<num[j]) num[j]=num[j-w[i]]+v[i];}
                else if(num[j-w[i]]!=-1&&num;[j]==-1)
                {num[j]=num[j-w[i]]+v[i];}
            }
        }
        if(num[m]!=-1)
        printf("The minimum amount of money in the piggy-bank is %d.\n",num[m]);
        else
            printf("This is impossible.\n");
    }
}

(PS:现开始把代码贴错了,感谢ambition神牛和MiYu神牛的指出,Orz)