天天看點

POJ3260---The Fewest Coins(混合背包)

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 V1, V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John 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 (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 V 1, V 2, ..., VN coins ( V 1, ... VN)

Line 3: N space-separated integers, respectively C 1, C 2, ..., 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.

混合背包問題,主要是求上界比較蛋疼。

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int inf = 1 << 18;
int v[maxn], c[maxn];
int dp1[20005], dp2[20005];
int n, m;
void complete_pack(int *dp, int cost, int weight, int limit)
{
    for (int i = cost; i <= limit; i++)
        dp[i] = min(dp[i], dp[i - cost] + weight);
}
void zero_one_pack(int *dp, int cost, int weight, int limit)
{
    for (int i = limit; i >= cost; i--)
        dp[i] = min(dp[i], dp[i - cost] + weight);
}
void multi_pack(int *dp, int cost, int weight, int limit, int amount)
{
    if (cost * amount >= limit) {
        complete_pack(dp, cost, weight, limit);
        return;
    }
    int k = 1;
    while (k < amount) {
        zero_one_pack(dp, k * cost, k * weight, limit);
        amount -= k;
        k <<= 1;
    }
    zero_one_pack(dp, amount * cost, amount * weight, limit);
}
int main()
{
    while (scanf("%d%d", &n, &m) != EOF) {
        for (int i = 1; i <= n; i++)
            scanf("%d", &v[i]);
        for (int i = 1; i <= n; i++)
            scanf("%d", &c[i]);
        for (int i = 0; i <= m + 10000; i++)
            dp1[i] = dp2[i] = inf;
        dp1[0] = dp2[0] = 0;
        for (int i = 1; i <= n; i++) {
            complete_pack(dp2, v[i], 1, m + 10000);   //找的錢是完全背包
            multi_pack(dp1, v[i], 1, m + 10000, c[i]); //付的錢是多重背包
        }
        int ans = inf;
        for (int i = m; i <= m + 10000; i++) {
            if (dp1[i] + dp2[i - m] < ans)
                ans = dp1[i] + dp2[i - m];
        }
        printf("%d\n", ans == inf ? -1 : ans);
    }
    return 0;
}