HDU 3594 The trouble of Xiaoqian 混合背包问题

The trouble of Xiaoqian

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


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
此题为多重背包和完全背包的混合,可分开求,在求最小值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll dpf[20006],dps[20006];
ll a[105],b;
ll ans,pos,n,m;
int main()
{
    int count=1;
    while(scanf("%lld%lld",&n,&m) && n+m)
    {
        for(int i=0;i<20003;i++)
        {
            dps[i]=dpf[i]=INF;
        }
        dps[0]=dpf[0]=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&b);
            for(int k=1;b;k*=2)
            {
                if(b<k) k=b;
                for(int j=20000;j>=k*a[i];j--)
                {
                    dpf[j]=min(dpf[j],dpf[j-k*a[i]]+k);
                }
                b-=k;
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=a[i];j<=20000;j++)
            {
                dps[j]=min(dps[j],dps[j-a[i]]+1);
            }
        }
        ans=INF;
        for(int i=m;i<=20000;i++)
        {
            ans=min(ans,dpf[i]+dps[i-m]);
        }
        if(ans==INF) ans=-1;
        printf("Case %d: %lld
",count++,ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7192966.html