hdu 3591 The trouble of Xiaoqian(多重背包)

来源:互联网 发布:不是淘宝卖家能贷款吗 编辑:程序博客网 时间:2024/06/08 20:13

The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1514 Accepted Submission(s): 520

Problem Description
In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.)
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, …, VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, …., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once.

Input
There are several test cases in the input.
Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, …, VN coins (V1, …VN)
Line 3: N space-separated integers, respectively C1, C2, …, CN
The end of the input is a double 0.

Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.

Sample Input
3 70
5 25 50
5 2 1
0 0

Sample Output
Case 1: 3

分析:这是一道典型的背包问题,因为xiaoqian的钱的个数是给定的所以使用多重背包,商店的钱币个数是unlimited所以使用完全背包
需要注意的是使用01背包时是dp[v]与dp[v-cost]+sum比求最小而不是与dp[i-cost]+1求最小!

#include <stdio.h>#include <stdlib.h>#define inf 1000000int f1[20005],f2[20005];int v[200],c[200],V,T;int Min(int x,int y){    if(x>y)        return y;    else        return x;}void ZeroOne(int dp[],int cost,int totle)//01背包{    int v;    for (v=20000; v>=cost; v--) {        dp[v]=Min(dp[v-cost]+totle, dp[v]);    }}void Complete(int dp[],int cost)//完全背包{    int v;    for (v=cost; v<=20000; v++) {        dp[v]=Min(dp[v], dp[v-cost]+1);    }}void Multiple(int dp[],int cost,int amount)//多重背包{    if(cost*amount>=20000)    {        Complete(dp, cost);        return;    }    int k=1;    while (k<amount) {        ZeroOne(dp, k*cost, k);        amount-=k;        k*=2;    }    ZeroOne(dp, amount*cost, amount);}void init()//f1,f2进行初始化{    int i;    f1[0]=f2[0]=0;    for (i=1; i<=20004; i++) {        f1[i]=inf,f2[i]=inf;    }}int main(){    int N,count=0;    while (scanf("%d%d",&N,&T)!=EOF&&N+T) {        int i;        V=0;        init();        for (i=1; i<=N; i++) {            scanf("%d",&v[i]);        }        for (i=1; i<=N; i++) {            scanf("%d",&c[i]);            V+=c[i]*v[i];        }        for (i=1; i<=N; i++) {            Multiple(f1, v[i], c[i]);        }        for (i=1; i<=N; i++) {            Complete(f2, v[i]);        }        int result=f1[T];        for (i=T+1; i<=20000; i++) {            result=Min(f1[i]+f2[i-T], result);        }        if(result==inf)            printf("Case %d: -1\n",++count);        else            printf("Case %d: %d\n",++count,result);    }    return 0;}
0 0
原创粉丝点击