The trouble of Xiaoqian

 
O - The trouble of Xiaoqian
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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>
#define Maxn 999999
using namespace std;

int w[105],num[105],sum;
int dp1[22000];
int dp2[22000];


int main()
{
    int i,k,j;
    int n,T,cas=0;
    while(scanf("%d%d",&n,&T)!=EOF&&(n||T))
    {
        for(i=0;i<n;i++)
            scanf("%d",&w[i]);
        for(i=0;i<n;i++)
            scanf("%d",&num[i]);

        memset(dp2,63,sizeof(dp2));
        dp2[0]=0;
        for(i=0;i<n;i++)
            for(j=w[i];j<=20000;j++)
                dp2[j]=min(dp2[j],dp2[j-w[i]]+1);

        memset(dp1,63,sizeof(dp1));
        dp1[0]=0;
        for(i=0;i<n;i++)
        {
            int tmp=num[i];
            for(k=1;k<=tmp;k*=2)
            {
                if(num[i]<k) k=num[i];
                int s=k*w[i];
                for(j=20000;j>=s;j--)
                    dp1[j]=min(dp1[j],dp1[j-s]+k);
                num[i]-=k;
                if(num[i]==0)
                    break;
            }
} cout<<"Case "<<++cas<<": "; int ans=Maxn; for(i=T;i<=20000;i++) ans=min(ans,dp1[i]+dp2[i-T]); if(ans==Maxn) printf("-1\n"); else printf("%d\n",ans); } return 0; }

posted on 2013-08-04 18:10  暴力的轮胎  阅读(299)  评论(0编辑  收藏  举报

导航