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

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 V1V2, ..., VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1C2 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 V1V2, ..., VN coins (V1, ...VN
Line 3: N space-separated integers, respectively C1C2, ..., 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

  1. 3 70
  2. 5 25 50
  3. 5 2 1

Sample Output

  1. 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.

Source


题意:FJ每种硬币有限,售货员无限,最小化交易用的硬币数

FJ多重背包,售货员完全背包
min一下f[i+m]+d[i]
NOTICE:体积选多大呢?
有一个证明,如果John的付款数大于了maxv*maxv+m,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。
看不懂算了
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. const int N=,M=*+1e4+,INF=1e9;
  8. int read(){
  9. char c=getchar();int x=,f=;
  10. while(c<''||c>''){if(c=='-')f=-; c=getchar();}
  11. while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
  12. return x*f;
  13. }
  14. int n,m,om,v[N],c[N],ans=INF,mxv=;
  15. int f[M],d[M];
  16. inline void zp(int v,int w){
  17. for(int j=m;j>=v;j--) f[j]=min(f[j],f[j-v]+w);
  18. }
  19. inline void cp(int v){
  20. for(int j=v;j<=m;j++) f[j]=min(f[j],f[j-v]+);
  21. }
  22. inline void mp(int v,int c){
  23. if(v*c>=m){cp(v);return;}
  24. int k=;
  25. while(k<c){
  26. zp(k*v,k);
  27. c-=k;
  28. k*=;
  29. }
  30. zp(c*v,c);
  31. }
  32. int main(){
  33. n=read();m=om=read();
  34. for(int i=;i<=n;i++) v[i]=read(),mxv=max(mxv,v[i]);m+=mxv*mxv;
  35. for(int i=;i<=n;i++) c[i]=read();
  36.  
  37. for(int i=;i<=m;i++) d[i]=INF;d[]=;
  38. for(int i=;i<=n;i++)
  39. for(int j=v[i];j<=m;j++)
  40. d[j]=min(d[j],d[j-v[i]]+);
  41. for(int i=;i<=m;i++) f[i]=INF;f[]=;
  42. for(int i=;i<=n;i++) mp(v[i],c[i]);
  43. for(int i=;i<=m-om;i++)
  44. if(f[i+om]+d[i]<ans) ans=f[i+om]+d[i];
  45. if(ans>=INF) printf("-1");
  46. else printf("%d",ans);
  47. }
 

POJ3260The 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. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

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

  4. The Fewest Coins POJ - 3260

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

  5. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  6. POJ3260:The Fewest Coins(混合背包)

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

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

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

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

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

  9. 完全背包和多重背包的混合 F - The Fewest Coins

    http://poj.org/problem?id=3260 这个题目有点小难,我开始没什么头绪,感觉很乱. 后来看了题解,感觉豁然开朗. 题目大意:就是这个人去买东西,东西的价格是T,这个人拥有的纸 ...

随机推荐

  1. jquery messagetip信息语提示控件

    编写原因: 作为提示框,jquery有个messagebox的控件,也就是弹出的提示框.但这个控件如果不是用在需要确认的时候,单单警告提示.消息提示.失败提示时,用户还需要去点下确认,有时这操作还是挺 ...

  2. 【代码笔记】iOS-单例

    一,工程图. 二,代码. NetManager.h #import <Foundation/Foundation.h> @interface NetManager : NSObject + ...

  3. Swift 初步了解

    Swift 初步了解 前言: 本篇博客会结合OC对Swift进行简单介绍. OC 用NSLog输出日志 NSLog(@"旭宝爱吃鱼"); Swift 用print输出日志 prin ...

  4. [读书笔记] CSS权威指南1: 选择器

    通配选择器 可以与任何元素匹配,就像是一个通配符 /*每一个元素的字体都设置为红色*/ * { color: red; } 元素选择器 指示文档元素的选择器. /*为body的字体设置为红色*/ bo ...

  5. cocoapods遇到的问题 (pod: command not found的问题)

    在使用CocoaPod为项目添加第三方类库时,出现了-bash: pod: command not found的问题: 在网上看到了一位哥的方法:确实有效:

  6. 回到过去美好的时光——源代码版本管理Always Use source code Control

    Don't Repeat Yourself Don't Repeat Yourself,这是程序员修炼之道中的经典名言.源代码版本管理对程序员来说是非常重要的工作.因为它让你能够回到过去,而不用做重复 ...

  7. Asp.net禁用页面缓存的方法总结

    1.在Asp页面首部<head>加入 复制代码 代码如下:   Response.Buffer   =   True      Response.ExpiresAbsolute   =   ...

  8. nav布局 在线演示 DIVCSS5

    <!doctype html> nav布局 在线演示 DIVCSS5 传统 div ul li布局导航条效果 网站首页 HTML教程 HTML入门 HTML5教程 CSS教程 CSS入门 ...

  9. 测试必备技能系列1 :通过mysql命令进行脚本数据导入

    老徐,分享测试项目中实际能解决问题的干货!   今日分享: 如何通过mysql命令行,导入mysql脚本文件数据?   ----- 解决实际的问题: 工作过程中,经常需要导入mysql脚本文件 很多同 ...

  10. 让UNION与ORDER BY并存于SQL语句当中

    在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下: SELECT [Id],[Name],[Comment] FROM [Product1] UNIO ...