POJ3260The 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 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 V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., CN
Output
Sample Input
3 70
5 25 50
5 2 1
Sample Output
3
Hint
Source
题意:FJ每种硬币有限,售货员无限,最小化交易用的硬币数
FJ多重背包,售货员完全背包
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,M=*+1e4+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,om,v[N],c[N],ans=INF,mxv=;
int f[M],d[M];
inline void zp(int v,int w){
for(int j=m;j>=v;j--) f[j]=min(f[j],f[j-v]+w);
}
inline void cp(int v){
for(int j=v;j<=m;j++) f[j]=min(f[j],f[j-v]+);
}
inline void mp(int v,int c){
if(v*c>=m){cp(v);return;}
int k=;
while(k<c){
zp(k*v,k);
c-=k;
k*=;
}
zp(c*v,c);
}
int main(){
n=read();m=om=read();
for(int i=;i<=n;i++) v[i]=read(),mxv=max(mxv,v[i]);m+=mxv*mxv;
for(int i=;i<=n;i++) c[i]=read(); for(int i=;i<=m;i++) d[i]=INF;d[]=;
for(int i=;i<=n;i++)
for(int j=v[i];j<=m;j++)
d[j]=min(d[j],d[j-v[i]]+);
for(int i=;i<=m;i++) f[i]=INF;f[]=;
for(int i=;i<=n;i++) mp(v[i],c[i]);
for(int i=;i<=m-om;i++)
if(f[i+om]+d[i]<ans) ans=f[i+om]+d[i];
if(ans>=INF) printf("-1");
else printf("%d",ans);
}
POJ3260The 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种货币 ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- The Fewest Coins POJ - 3260
The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- POJ3260:The Fewest Coins(混合背包)
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- 洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)
题目描述 Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always p ...
- POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...
- 完全背包和多重背包的混合 F - The Fewest Coins
http://poj.org/problem?id=3260 这个题目有点小难,我开始没什么头绪,感觉很乱. 后来看了题解,感觉豁然开朗. 题目大意:就是这个人去买东西,东西的价格是T,这个人拥有的纸 ...
随机推荐
- Delphi 获取系统时间后格式化输出
问题:客户现场程序运行提示时间格式不对导致的错误,原因是与开发环境及公司内部测试环境的日期格式不一致: 解决:统一强制转换: //引用单元:SysUtils //目的:实现跨环境兼容不同日期格式,如果 ...
- javascript --- Function模式
回调函数 在javascript中,当一个函数A作为另外一个函数B的其中一个参数时,则称A函数为回调函数,即A可以在函数B的运行周期内执行(开始,中间,结束). 举例来说,有一个函数用于生成node. ...
- curl命令学习笔记
下载文件并保存为默认文件名 curl -O http://www.xxx.com/xxx.exe 下载文件并保存为指定文件名 curl -o filename.exe http://www.xxx.c ...
- Understanding theory (1)
Source: verysmartbrothas.com It has been confusing since my first day as a PhD student about theory ...
- umeng 渠道统计 android
1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...
- jQuery导航切换功能
Css ------------------------------------------------------------------------------------------------ ...
- raw_input() 与 input() __ Python
这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同.举两个小例子. 1 >>> raw_input_A = raw_input(" ...
- php array 分页
$array=array("1","2","3","4","5","6",&qu ...
- 使用CSS3滤镜让图片反转颜色
CSS提供的滤镜也是一大亮点,我一直痴迷其中,有些滤镜的效果很有用,可是有些的滤镜效果可能只是为了玩玩儿,CSS常见的滤镜有这些:grayscale, blur, sepia,所有常见的过滤器.但是如 ...
- MapReduce实例-基于内容的推荐(一)
环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境 数据:下载的amazon产品共同采购网络元数据(需FQ下载)http://snap.stanford.edu/data/ ...