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 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
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
题意:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
思路:这题想了很久没有想出思路,虽然知道是背包,但是不知道该如何让运用,看了别人的代码,感觉人家的思路真心碉堡了,讲解在代码中
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int v[105],c[105],MAX,n,sum;
int dp[33333],inf = 100000000; void ZeroOnePack(int cost,int cnt)
{
int i;
for(i = sum+MAX; i>=cost; i--)
dp[i] = min(dp[i],dp[i-cost]+cnt);//找出最小数量的方案
} void CompletePack(int cost,int cnt)
{
int i;
for(i = sum+MAX+cost; i>=0; i--)
dp[i] = min(dp[i],dp[i-cost]+cnt);
} int MultiplePack()
{
int i,j,k;
for(i = 1; i<=sum+MAX; i++)
dp[i] = inf;
dp[0] = 0;//dp数组用来记录钱币数量
for(i = 1; i<=2*n; i++)
{
if(i<=n)//这是顾客购买时所给的钱的数量
{
k = 1;
while(k<c[i])
{
ZeroOnePack(k*v[i],k);
c[i]-=k;
k*=2;
}
ZeroOnePack(c[i]*v[i],c[i]);
}
else
CompletePack(-v[i-n],1);//只所以是负数,是因为这是老板找钱的数目
}
if(dp[sum]==inf)
return -1;
else
return dp[sum];
} int main()
{
int i;
while(~scanf("%d%d",&n,&sum))
{
MAX = 0;
for(i=1; i<=n; i++)
{
scanf("%d",&v[i]);
MAX = max(MAX,v[i]);
}
MAX*=MAX;//保证背包足够大
for(i=1; i<=n; i++)
scanf("%d",&c[i]);
printf("%d\n",MultiplePack());
} return 0;
}
POJ3260:The Fewest Coins(混合背包)的更多相关文章
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...
- 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...
- poj3260 The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- The Fewest Coins POJ - 3260
The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
随机推荐
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- JavaScript深拷贝和浅拷贝
1. 基本类型 和 对象类型 他们最大的区别就是在于他们的传值方式. 基本类型是传值 对象类型就是传引用. 这里复制一份obj叫做obj2, 这里修改了obj2的b为100 同时也修改了obj1.b. ...
- javasscript学习笔记 之 数组学习二 数组的所有方法
1.push() 和 pop() 栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...
- 使用Gradle构建Android应用内测版本
在开发应用的过程中,有时候需要比较当前线上版本和正在开发中的版本差异,目前的做法只能是在两个不同的设备上面安装线上版本和开发中的版本,因为当前版本在调试过程中会覆盖旧版本.本文通过使用gradle来构 ...
- Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
1.通过Oracle序列 -- Create sequence create sequence SEQ_DW_EWSYSTEM minvalue 1 maxvalue 9999999999999999 ...
- Javscript中的null和undefined
1.null是JavaScript关键字,含义是“非对象”,它可以表示数字.字符串和对象是“无值”的. var x = null; typeof x ;//返回“object” var x=null, ...
- SharpZipLib 压缩文档下载
using ICSharpCode.SharpZipLib.Zip; Response.Clear(); Response.ClearContent(); Response.ClearHeaders( ...
- Nutch的日志系统
一.Nutch日志实现方式 1.Nutch使用slf4j作为日志接口,使用log4j作为具体实现.关于二者的基础,请参考 http://blog.csdn.net/jediael_lu/article ...
- javascript数组方法鉴赏一
创建数组 如果你习惯了用 new 来实例化对象的形式,那么在js中一定会疑惑,可选的参数数量代表的意义截然不同. new Array(size);//传一个参数的时候分两种情况,size是正整数时代表 ...
- php如何返回一个image文件
The important points is that you must send a Content-Type header. Also, you must be careful not incl ...