题目代号:POJ 3260

题目链接:http://poj.org/problem?id=3260

The Fewest Coins

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6715 Accepted: 2072

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 V1, V2, ..., VN coins (V1, ...VN)

Line 3: N space-separated integers, respectively C1, C2, ..., 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.

题目大意:给你N种面值的钱以及买家所拥有的数量(注意:卖家的没种面值的钱是无限的),要买T价值的东西,问:要使给出的钱和收到的钱数量最小,然后求总和就是答案。

题目分析:对于买家来说,这是一个多重背包问题,对于卖家来说,这是一个完全背包问题,所以这个题是多重背包和完全背包组合而成的混合背包问题,由于背包问题都是由01背包衍生和优化出来的,所以需要对于01背包的思想完全掌握和熟练运用。然后另一个问题就是这个背包容量的上限问题,在我们生活之中如果需要买1269元的东西那么我们需要给出12张100元面值的,1张50元面值的,1张10元面值的,1张5元面值的,4张1元面值的,合计19张,那么如果按照题目中的要求我可以付款13~18张的100面值的钱,18显然比19要少,但是给了超过1300元的面值的100元都会被卖家原样找回,这很显然是没有必要的,因为在最优的答案之中找的钱不会超过100元,这个题目中也是这样,然后根据抽屉原理,多重背包容量的上限为T+max*max(max即为最大面值的钱),完全背包的上限为T+max。(别问我,我也不知道怎么来的,记住就好了,实在不会那就数组开大点。。。)

AC代码:

# include <bits/stdc++.h>
using namespace std;
# define mem(a,b) memset(a,b,sizeof(a))
# define IOS ios::sync_with_stdio(false);
# define FO(i,n,a) for(int i=n; i>=a; --i)
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define MAX 0x7fffffffffffff
# define INF 0x3f3f3f3f
# define MOD 1000000007
/// 123456789
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef unsigned long long ULL;
typedef long long LL;
///coding...................................
const int MAXM=30005;
int n,m,c[105],v[105],sum;
int b[MAXM],a[MAXM]; void complete_bag(int *dp,int vis) {
FOR(i,vis,sum)
dp[i]=min(dp[i],dp[i-vis]+1);
} void zero_one_bag(int *dp,int vis,int cost) {
FO(i,sum,vis)
dp[i]=min(dp[i],dp[i-vis]+cost);
} void mult_bag(int *dp,int vis,int cost) {
if(vis*cost>=sum)complete_bag(dp,vis);
else{
int cnt=1;
while(cnt<=cost){
zero_one_bag(dp,vis*cnt,cnt);
cost-=cnt;
cnt<<=1;
}
if(cost)zero_one_bag(dp,vis*cost,cost);
}
} int main()
{
IOS
#ifdef FLAG
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif /// FLAG
while(cin>>n>>m) {
sum=0;
for(int i=0;i<n;i++)
cin>>v[i],sum=max(sum,v[i]);
for(int i=0;i<n;i++)cin>>c[i];
sum=m+sum*sum;
for(int i=1;i<=sum;i++)a[i]=b[i]=INF;
b[0]=a[0]=0;
for(int i=0;i<n;i++)complete_bag(a,v[i]);
for(int i=0;i<n;i++)mult_bag(b,v[i],c[i]);
int ans=INF;
for(int i=m;i<=sum;i++)
if(b[i]+a[i-m]<ans)
ans=b[i]+a[i-m];
if(ans==INF)puts("-1");
else cout<<ans<<endl;
}
return 0;
}

POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)的更多相关文章

  1. POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)

    题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...

  2. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  3. poj 3260 The Fewest Coins

    // 转载自http://blog.163.com/benz_/blog/static/18684203020115721917109/算法不难看出,就是一个无限背包+多重背包.问题在于背包的范围.设 ...

  4. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

  5. POJ 3260 The Fewest Coins(背包问题)

    [题目链接] http://poj.org/problem?id=3260 [题目大意] 给出你拥有的货币种类和每种的数量,商店拥有的货币数量是无限的, 问你买一个价值为m的物品,最少的货币流通数量为 ...

  6. codevs 3269 混合背包(复习混合背包)

    传送门 [题目大意]给出物品的数量.-1为无限个. [思路]混合背包.... [code] #include<iostream> #include<cstdio> #inclu ...

  7. The Fewest Coins POJ - 3260

    The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...

  8. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  9. POJ 3260 多重背包+完全背包

    前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...

随机推荐

  1. jquery validate 自定义校验方法

    1.引入JS jquery.min.js jquery.validate.min.js messages_zh.min.js 2.添加验证方法,第一个参数为验证方法的名称,第二个参数为验证方法. $. ...

  2. sqlserver with(nolock)而mysql 不需nolock

    nolock 是 SQL Server 特有的功能. 例如:对于一个表 A,更新了一行,还没有commit,这时再select * from A 就会死锁.用select * from A(noloc ...

  3. Python字符串和正则表达式中的反斜杠('\')问题

    在Python普通字符串中 在Python中,我们用'\'来转义某些普通字符,使其成为特殊字符,比如 In [1]: print('abc\ndef') # '\n'具有换行的作用 abc defg ...

  4. java复习(1)面向对象

    一.面向对象的概念 ----------------------------------------------------- 1.理解面向对象:(1)面向对象是相对于面向过程的语言 (2)面向对象和 ...

  5. 解析html,提取元素参数

    r = s.get(loginurl, verify=False) dom = etree.HTML(r.content.decode("utf-8")) try: result[ ...

  6. 一道有关#define的题

    题目是:查看以下代码,问结果是什么? 结果是打印出“array:16345678910”吗? #include "stdafx.h" #include <iostream&g ...

  7. iconv_open - 分配一个字符集转换的描述符

    总览 (SYNOPSIS) #include <iconv.h> iconv_t iconv_open (const char* tocode, const char* fromcode) ...

  8. jmeter之Ramp-up Period(in seconds)

    [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行.每个线程会在上一个线程启动后10秒(100/10)启动 ...

  9. 一键部署lnmp脚本

    先下载好nginx安装包,解包之后可以执行下面的脚本,一键部署 cd nginx-1.12.2 useradd -s /sbin/nologin nginx./configuremakemake in ...

  10. Java并发编程实战 第11章 性能与可伸缩性

    关于性能 性能的衡量标准有很多,如: 服务时间,等待时间用来衡量程序的"运行速度""多快". 吞吐量,生产量用于衡量程序的"处理能力",能够 ...