Description

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2 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 (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

Input

Line 1: Two space-separated integers: N and T
Line 2: N space-separated integers, respectively V1V2, ..., VN coins (V1, ...VN
Line 3: N space-separated integers, respectively C1C2, ..., CN

Output

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

Sample Input

3 70
5 25 50
5 2 1

Sample Output

3

Hint

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.
 
题意:John去买东西,东西的价格是T(1 <= T <= 10000),John所在的地方有n(1 <= n <= 100)种的硬币,面值分别为V1, V2, ..., Vn (1 <= Vi <= 120)。John带了C1枚面值为V1的硬币,C2枚面值为V2的硬币,...,Cn枚面值为Vn的硬币(0 <= Ci <= 10000)。售货员那里每种硬币都有无限多个。问为了支付这个T,John给售货员的硬币数目加上售货员找回的零钱的硬币数目最少是多少。如果无法支付 T,输出-1 。

解法:支付时硬币数量有限制,为多重背包问题,通过二进制方法转化为01背包求解。找零时,硬币数量无限制,为完全背包问题。对两问题分别求解,然后找出差额为T时,两者和的最小值即为所示。
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define met(a,b) (memset(a,b,sizeof(a)))
#define N 20000
#define INF 0x3f3f3f3f

int V[110], C[110];
int v[N], c[N], dp[N];
int n, T, sum, k, Update;

///Update为更新的范围, 价值T最大为10000,而V[i]最大为120, 因此Update为10200便可以

void Init() ///将多重背包利用倍增法,转化为01背包
{           ///并解决一些初始化的问题
    int i, j;

    k=1;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=C[i]; j*=2)
        {
            v[k] = V[i]*j;
            c[k++] = j;
            C[i] -= j;
        }
        if(C[i])
        {
            v[k] = V[i]*C[i];
            c[k++] = C[i];
            C[i] = 0;
        }
    }
    k--;

    for(i=0; i<=Update; i++)
        dp[i] = INF;
}

void First()
{
    int i, j;

    dp[0] = 0;
    for(i=1; i<=k; i++)
    {
        for(j=Update; j>=v[i]; j--) 
            dp[j] = min(dp[j], dp[j-v[i]]+c[i]);
    }
}

int Secound()
{
    int i, j;

    for(i=1; i<=n; i++)
    {
        for(j=Update-V[i]; j>=0; j--)
        {///看好哦, 这里是加号,也就是从后面更新过来的, 于是第二重循环要逆着来
            dp[j] = min(dp[j], dp[j+V[i]]+1);
        }
    }

    return dp[T];
}

int main()
{
    while(scanf("%d%d", &n, &T)!=EOF)
    {
        int i;
        sum=0;
        for(i=1; i<=n; i++)
            scanf("%d", &V[i]);
        for(i=1; i<=n; i++)
        {
            scanf("%d", &C[i]);
            sum += V[i]*C[i];
        }
        Update=10200;

        Init(); ///初始化
        First();///01背包
        int ans = Secound(); ///完全背包

        if(T>sum) printf("-1\n");
        else
        {
            if(ans==INF) ///如果ans==INF说明并没有更新到dp[T],不能兑换到
                printf("-1\n");
            else
                printf("%d\n", dp[T]);
        }

    }
    return 0;
}

 

posted on 2016-06-01 15:55  栀蓝  阅读(465)  评论(0编辑  收藏  举报

levels of contents