题目代号: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. shiro登陆认证

    1.LoginController @RequestMapping(method = RequestMethod.POST) public String login(User user, HttpSe ...

  2. kubernetes集群node加入不了master错误处理

    #如果node加入不了master或者加入成功但是,在master中显示不出来.排查错误:1. 运行,kubelet, 查看日志,一般是kubelet的运行和docker启动方式不匹配.调整:vim  ...

  3. 使用注解方式搭建SpringMVC

    1.以前搭建Spring MVC 框架一般都使用配置文件的方式进行,相对比较繁琐.spring 提供了使用注解方式搭建Spring MVC 框架的方式,方便简洁.使用Spring IOC 作为根容器管 ...

  4. 几个关于json序列化 的注解

    一.@JsonProperty 1.此注解用于属性上,作用是把该属性的名称序列化为另外一个名称.例如: @JsonProperty("name") private String N ...

  5. XOR on segment(线段树区间异或更新)

    原题传送门 本题大意:给定n个数字和m个操作,操作共有两种,第一种是求解区间l到r上元素的和,第二种是将区间l到r的元素都异或一个x,作为某个位置的新值. 很容易想到线段树维护区间和,但是我们发现,在 ...

  6. 洛谷 P1134 阶乘问题 题解

    题面 很裸的边取模边乘.注意因为进位的原因模数应该比较大: 另外,这道题是一道标准的分块打表例题(那样的话数据就可以更大了),可以用来练习分块打表: #include<bits/stdc++.h ...

  7. mysql表优化

    一.定期分析表 ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] 二.定期检查表 CHECK TABLE tbl_nam ...

  8. 解决 find: 路径必须在表达式之前:

    通配符前面加\ 转义 或者 换个目录操作

  9. java复习(1)

    这几天开学,很多知识点还很生疏,这两天先把java基础复习一下,有段时间没有写博客了,今天就先谈谈进制转换吧. 1.二进制数的原码,补码和反码 1):对于正数的原码,补码和反码均是相同的,这里不讨论了 ...

  10. 2019-11-29-dotnet-core-输出调试信息到-DebugView-软件

    title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-11-29 10:14:3 ...