[USACO06DEC] The Fewest Coins G(混合背包问题)

[USACO06DEC] The Fewest Coins G

题目描述

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).

农夫 John 想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。

John 想要买价值为 T T T 的东西。有 N N N 1 ≤ N ≤ 100 1 \le N \le 100 1N100)种货币参与流通,面值分别为 V 1 , V 2 , … , V N V_1,V_2,\dots,V_N V1,V2,,VN 1 ≤ V i ≤ 120 1 \le V_i \le 120 1Vi120)。John 有 C i C_i Ci 个面值为 V i V_i Vi 的硬币( 0 ≤ C i ≤ 1 0 4 0 \le C_i \le 10 ^ 4 0Ci104)。

我们假设店主有无限多的硬币, 并总按最优方案找零。注意无解输出 -1

输入格式

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

输出格式

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.

样例 #1

样例输入 #1

3 70
5 25 50
5 2 1

样例输出 #1

3

提示

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<iostream>
#include<cstring>
#define int long long
using namespace std;

const int N = 510*510;

int f[N],g[N];
int v[N],c[N];
int n,T;

signed main(){
    cin>>n>>T;
    
    for(int i=1;i<=n;i++){
        cin>>v[i];
    }
    
    for(int i=1;i<=n;i++)cin>>c[i];
    
    memset(f,0x3f,sizeof f);
    memset(g,0x3f,sizeof g);
    g[0]=0;
    f[0]=0;
    
    for(int i=1;i<=n;i++){
        for(int k=1;k<=c[i];k<<=1){
            for(int j=N;j>=k*v[i];j--){
                f[j]=min(f[j],f[j-k*v[i]]+k);
            }
            c[i]-=k;
        }
        if(c[i]){
            for(int j=N;j>=v[i]*c[i];j--){
                f[j]=min(f[j],f[j-v[i]*c[i]]+v[i]);
            }
        }
    }
    
    for(int i=1;i<=n;i++){
        for(int j=v[i];j<=N-T;j++){
            g[j]=min(g[j],g[j-v[i]]+1);
        }
    }
    
    int res=2e9;
    
    for(int i=0;i<=N-T;i++){
        res=min(res,f[i]+g[i+T]);
    }
    
    if(res==2e9){
        puts("-1");
    }else{
        cout<<res;
    }
    
    return 0;
}
  • 43
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值