HDOJ 1114 Piggy-Bank

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Author: Tanky Woo
// HDOJ 1114
 
 
#include <iostream>
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; i<nPack; ++i)
        {
            scanf("%d %d", &value[i], &weight[i]);
        }
        for(int i=0; i<=nVolume; ++i)
            record[i] = INF;
        record[0] = 0;
        for(int i=0; i<nPack; ++i)
            for(int j=weight[i]; j<=nVolume; ++j)
                if(record[j]>record[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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<stdio.h>
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)

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

《HDOJ 1114 Piggy-Bank》有13个想法

  1. #include
    using namespace std;

    int nCases = 1;
    int nPack, nVolume1, nVolume2, nVolume;
    int weight[10], value[10];
    int record[11];
    const int INF = 0;
    int main()
    {
    //freopen(“input.txt”, “r”, stdin);

    //scanf(“%d”, &nCases);
    while(nCases–)
    {
    printf(“Please input the nVolume1 and nVolume2 \n”);
    scanf(“%d %d”, &nVolume1, &nVolume2);
    nVolume = nVolume2-nVolume1;
    printf(“Please input the nPack \n”);
    scanf(“%d”, &nPack);
    printf(“Please input the weight and value for each Pack \n”);
    for(int i=0; i<nPack; ++i)
    {
    scanf("%d %d", &weight[i], &value[i]);
    }
    for(int i=0; i<=nVolume; ++i)
    record[i] = INF;
    record[0] = 0;

    for(int i=0; i<nPack; ++i)
    for(int j=weight[i]; j<=nVolume; ++j)
    if(record[j]<record[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 max amount of money in the piggy-bank is %d.\n",record[nVolume]);

    }
    return 0;
    }

  2. 而且是 逆序的, “想必大家看出了和01背包的区别,这里的内循环是顺序的,而01背包是逆序的。” 上面代码还是逆序的…………………..出自”[url]http://www.wutianqi.com/?p=539&cpage=1#comment-134[/url]”完全背包

发表评论

电子邮件地址不会被公开。 必填项已用*标注