POJ 3260 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.

解题报告:

题意和思路就直接贴EaShion1994博客里的了:http://blog.csdn.net/EaShion1994/article/details/43744767

题意:定义一共有N种货币,对应V1….Vn,N种价格。老农要拿一些钱去买价格为T的东西,老农有C1…..CN个对应货币,售货员有无限个货币。求如何才能使老农拿的硬币数与找零的硬币数和最小。

算法:
老农上界:maxv*maxv+T;售货员上界:maxv*maxv
证明(参考网上):如果John的付款数大于了maxv*maxv+T,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。

老农:多重背包
售货员:完全背包

。至于上界为什么要这样确定,还不是很懂。但是如果不确定上界,就开得大一些吧。

#include <cstdio>
#include <cstring>
#define min(a, b) (a < b ? a : b)
#define MAXV 121
#define MAXT 10000
//dp[i]表示dp凑够i元所用的最小货币数。
int farmer[MAXV * MAXV + MAXT], shopkeeper[MAXV * MAXV];
int n, t, v[MAXV], num[MAXV], i, ans, maxv;

void ZeroOnePack(int *dp, int cost, int V, int cnt) {
    for (int j = V; j >= cost; j --) {
        dp[j] = min(dp[j], dp[j-cost] + cnt);
    }
}
void CompletePack(int *dp, int cost, int V) {
    for (int j = cost; j <= V; j ++) {
        dp[j] = min(dp[j], dp[j-cost] + 1);
    }
}
void MultiPack(int *dp, int cost, int n, int V) {
    if(cost * n >= V - cost + 1) {
        CompletePack(dp, cost, V);
    } else {
        int k = 1;
        while (k < n) {
            ZeroOnePack(dp, k * cost, V, k);
            n -= k;
            k *= 2;
        }
        ZeroOnePack(dp, n * cost, V, n);
    }
}
int main() {
    while (scanf("%d%d", &n, &t) != EOF) {
        //init
        ans = 0x7ffffff, maxv = 0;
        memset(farmer, 0x3f, sizeof(farmer)), farmer[0] = 0;
        memset(shopkeeper, 0x3f, sizeof(shopkeeper)), shopkeeper[0] = 0;
        for (i = 0; i < n; i ++) {
            scanf("%d", v + i);
            if(maxv < v[i]) maxv = v[i];
        }
        maxv *= maxv;
        for (i = 0; i < n; i ++) {
            scanf("%d", num + i);
        }
        //solve
        for (i = 0; i < n; i ++) {
            MultiPack(farmer, v[i], num[i], maxv + t);
            CompletePack(shopkeeper, v[i], maxv);
        }
        for (i = 0; i <= maxv; i ++) {
            if(ans > farmer[i + t] + shopkeeper[i]) {
                ans = farmer[i + t] + shopkeeper[i];
            }
        }
        //output
        if(ans == 0x7ffffff) {
            printf("-1\n");
        } else {
            printf("%d\n", ans);
        }
    }
    return 0;
}

当然这道题的价值和代价是相等的,这让我们又想起多重背包特例的O(NV)算法,用一个数组来统计货币的使用次数。代码就贴上来供大家参考:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
#define maxn 15005
#define inf 0x7fffff
pair <int ,int > p;
int dp1[maxn];
int dp2[maxn];
int v[105];
int used[maxn];
int c[maxn];
int main()
{
	int k,t,n;
	while(scanf("%d%d",&n,&t)!=EOF){
		for(int i=1;i<=n;i++){
			scanf("%d",&v[i]);
		}
		for(int j=1;j<=n;j++){
            scanf("%d",&c[j]);
		}
		for(int i=1;i<maxn;i++){
			dp1[i]=dp2[i]=inf;
		}
		dp1[0]=dp2[0]=0;
		for(int i=1;i<=n;i++){
			memset(used,0,sizeof(used));
			for(int j=v[i];j<maxn;j++){
				if(dp1[j-v[i]]+1<dp1[j]&&used[j-v[i]]<c[i]){
					dp1[j]=dp1[j-v[i]]+1;
					used[j]=used[j-v[i]]+1;
				}
			}
		}
		for(int i = 1;i <= n;i++){
			for(int j = v[i];j < maxn;j++){
				dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
			}
		}
		int ans = inf;
		for(int i = t; i < maxn;i++){
			ans = min(ans,dp1[i]+dp2[i-t]);
		}
		if(ans >= inf)
			ans=-1;
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值