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

3 70
5 25 50
5 2 1

Sample Output

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来代替。证毕。
看不懂算了
#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[背包]的更多相关文章

  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. 从0开始学angularjs-笔记03

    大家好,今天上班第一天,可能大家都不是很想上班吧,我也是一样啦---不想上班就来继续写我的angualrjs教程,造福大家吧!! 今天的主要讲解部分有以下几点:1.演示一个完整的项目结构  2.$sc ...

  2. jquery图片查看插件,支持旋转、放大、缩小、拖拽、缩略图(仿qq图片查看)

    最近做了一个jquery图片查看的插件,目的是能精确查看图片的详情,插件支持图片旋转.放大.缩小.拖拽.缩略图显示,界面效果是按照window的qq查看图片功能写的,当然不尽相同. 具体功能: 1. ...

  3. sharepoint2010升级到sharepoint2013的升级步骤和过程

  4. 让background的图片不随着view的大小而改变

    方法是在drawable文件中定义一个背景的xml文件. <?xml version="1.0" encoding="utf-8"?> <bi ...

  5. CocoaPods常用终端命令及Profile文件简单介绍

    Pod常用终端命令 pod init 创建pod文件 pod install 开始安装第三方框架,如果网上有更新,会安装最新的 pod install --verbose--no-repo-updat ...

  6. 操作系统开发系列—13.d.多进程 ●

    进程此时不仅是在运行而已,它可以随时被中断,可以在中断处理程序完成之后被恢复.进程此时已经有了两种状态:运行和睡眠.我们已经具备了处理多个进程的能力,只需要让其中一个进程处在运行态,其余进程处在睡眠态 ...

  7. RecyclerView的基本使用

    1.布局文件中使用 <android.support.v7.widget.RecyclerView android:id="@+id/recycleview" android ...

  8. NSPredicate

    NSPredicate 1. 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.通常被用来检索.替换那些符合某个模式的文本. 2. iOS中正则使用 有三种(NSPredicate, ...

  9. iOS 从Xcode看应用支持横竖屏

    要看一个应用是否支持横竖屏,要看Xcode里面的info.plist文件设置才清楚,每一个新建工程都会包含三个支持方式,即Supported interface orientations里面的就是 P ...

  10. samba服务配置

    使用yum或者apt-get安装 # yum install samba samba-client samba-swat Samba开发环境配置 Acl权限设置  [不是必须.只要保证web目录的所有 ...