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
V
1,
V
2, ...,
VN coins (
V
1, ...
VN)

Line 3: N space-separated integers, respectively
C
1,
C
2, ...,
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
 
题意:给出钱币的方案数和总价值,然后给出每种钱币的价值与数量,而老板也是每种钱币都拥有,但是没有数量限制,购买东西的时候,价值超过给定价值的话,老板会找钱,要求最小的交流钱币的数量
 
思路:这题想了很久没有想出思路,虽然知道是背包,但是不知道该如何让运用,看了别人的代码,感觉人家的思路真心碉堡了,讲解在代码中
 
#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(混合背包)的更多相关文章

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

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

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

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

  3. POJ3260 The Fewest Coins(混合背包)

    支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...

  4. 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

    题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...

  5. poj3260 The Fewest Coins

    Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...

  6. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  7. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  8. The Fewest Coins POJ - 3260

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

  9. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

随机推荐

  1. js将日期格式的时候转换成时间搓

    自己写的一个方法 function split_time(time){//将当前时间转换成时间搓  例如2013-09-11 12:12:12   var arr=time.split(" ...

  2. Dynamics CRM 常用 JS 方法集合

    JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...

  3. WebApi2官网学习记录--HttpClient Message Handlers

    在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...

  4. ASP.NET中过滤HTML字符串的两个方法

    先记下来,以作备用! /// <summary>去除HTML标记 /// /// </summary> /// <param name="Htmlstring& ...

  5. windows同一台电脑设置多个公钥与不同github帐号交互

    1 生成公钥 1. 安装git,从C:\Documents and Settings\Administrator\.ssh\目录打开 "Git Bash":2. 键入命令:ssh- ...

  6. asp.net 实现 tts

    之前用WinForm实现tts已经成功,就调用了下系统的类库.但我把相同的代码搬到asp.net上时却碰到了许多问题,查了好多网站.试过了很多方法,到现在算是做出了一部分吧. 之前调用微软的TTS是用 ...

  7. Jquery常用方法(转)

    原文:http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库.据 ...

  8. MySql函数应用

    -- 当前时间 now(); -- 查询结果串联(逗号) select group_concat(col_name) from table_name;

  9. javaWeb RSA加密使用

      加密算法在各个网站运用很平常,今天整理代码的时候看到了我们项目中运用了RSA加密,就了解了一下. 先简单说一下RSA加密算法原理,RSA算法基于一个十分简单的数论事实:将两个大质数相乘十分容易,但 ...

  10. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...