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 问在满足条件的情况下 ...
随机推荐
- MVC4 成员资格、 身份验证
SimpleMembership,成员资格提供程序. 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板 ASP.NET MVC 4 互联网模板中添加一些新的 ...
- Unity发送短信
闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...
- read(),write() 读/写文件
read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...
- Sass函数--颜色函数--HSL函数
HSL函数简介HSL颜色函数包括哪些具体的函数,所起的作用是什么: hsl($hue,$saturation,$lightness):通过色相(hue).饱和度(saturation)和亮度(ligh ...
- hdu 素数环
算法:搜索 题意:相邻的两个数之和是素数,别忘了最后一个,和第一个 Problem Description A ring is compose of n circles as shown in dia ...
- C++11 lambda表达式学习
lambda表达式是函数式编程的基础.咱对于函数式编程也没有足够的理解,因此这里不敢胡言乱语,有兴趣的可以自己查找相关资料看下.这里只是介绍C++11中的lambda表达式自己的认识.这里有参考文档h ...
- 关于C函数的参数个数的问题
本文引自:http://c.biancheng.net/cpp/html/1592.html 一个函数的参数的数目没有明确的限制,但是参数过多(例如超过8个)显然是一种不可取的编程风格.参数的数目直接 ...
- 条形码/二维码之开源利器ZXing图文介绍
全文目录: 基本介绍 二维码(比如:QRCode)的编码和解码演示 条形码(比如:EAN-13)的编码和解码演示 [一]. 基本介绍 : 1-1. ZXing是一个开源Java类库用于解析多种格式的条 ...
- phantomjs 渲染
phantomjs 可以将web页面渲染并保存为扩展名为PNG,GIF,JPEG,PDF的指定文件 render viewportSize可以改变可视窗体大小 zoomFactor调整缩放比例 cli ...
- [转]shell中 source命令即点空格后面再跟可执行文件的说明
这里记录的是在一个shell脚本里面使用. ./file.sh 和./file.sh 的区别,本文参考了http://www.lslnet.com/linux/dosc1/39/linux-28353 ...