HDU 3591 多重背包

给出N种钱币和M

给出N种钱币的面值和个数

NPC拿着这N些钱币去买价值M的物品,可以多付,然后被找零,找零的钱也为这些面值,但没有数量限制

问最少经手的钱币数量

对于NPC做一个付款多重背包

然后对于找零做一个完全背包

ans=Min(dp1[i]+dp2[i-m],ans);

#include "stdio.h"
#include "string.h"

int n,m;
int dp1[20010],dp2[20010],c[20010],v[20010];
void onezero_pack(int v,int k)
{
    int i;
    for (i=20000;i>=v;i--)
        if (dp1[i-v]!=-1 && (dp1[i-v]+k<dp1[i] || dp1[i]==-1) )
        dp1[i]=dp1[i-v]+k;
}

void complete_pack(int v)
{
    int i;
    for (i=v;i<=20000;i++)
        if (dp1[i-v]!=-1 && (dp1[i-v]+1<dp1[i] || dp1[i]==-1) )
        dp1[i]=dp1[i-v]+1;
}

void multiple_pack(int v,int c)
{
    int k;
    if (v*c>=20000)
        complete_pack(v);
    else
    {
        k=1;
        while (k<c)
        {
            onezero_pack(k*v,k);
            c-=k;
            k*=2;
        }
        if (c>0) onezero_pack(c*v,k);
    }
}

int Min(int a,int b)
{
    if (a<b) return a;
    else return b;
}
int main()
{
    int Case,i,j,ans;
    Case=0;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        if (n+m==0) break;
        for (i=1;i<=n;i++)
            scanf("%d",&v[i]);
        for (i=1;i<=n;i++)
            scanf("%d",&c[i]);

        memset(dp1,-1,sizeof(dp1));
        dp1[0]=0;

        for (i=1;i<=n;i++)
            multiple_pack(v[i],c[i]);

       memset(dp2,-1,sizeof(dp2));
        dp2[0]=0;
        for (i=1;i<=n;i++)
            for (j=0;j<=20000-v[i];j++)
            {
                if (dp2[j]!=-1 && (dp2[j]+1<dp2[j+v[i]] || dp2[j+v[i]]==-1) )
                    dp2[j+v[i]]=dp2[j]+1;
            }

        ans=0x3f3f3f3f;
        for (i=m;i<=20000;i++)
            if (dp1[i]!=-1 && dp2[i-m]!=-1)
                ans=Min(dp1[i]+dp2[i-m],ans);

        printf("Case %d: ",++Case);
        if (ans==0x3f3f3f3f)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-01 20:36:33

HDU 3591 多重背包的相关文章

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 5445 多重背包

Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1243    Accepted Submission(s): 368 Problem Description Few days before a game of orienteering, Bell came to a mathematician to sol

HDU 3591 (完全背包+二进制优化的多重背包)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2798    Accepted Submission(s): 972 Problem Description In the countr

HDU1171--Big Event in HDU(多重背包)

Big Event in HDU   Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1139 Accepted Submission(s): 444 Problem Description Nowadays, we all know that Computer College is the biggest department in HD

hdu 1059 (多重背包) Dividing

这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的两份. 联想到多重背包,稍微用二进制优化一下.(最近身体不适,压力山大啊) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define inf 70000 5 using namespace s

Big Event in HDU(多重背包套用模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28483    Accepted Submission(s): 10027 Problem Description Nowadays, we all know

hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17930    Accepted

HDU 2191 多重背包

悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22865    Accepted Submission(s): 9661 Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市