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 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN)
Line 3: N space-separated integers, respectively C1, C2, ..., 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

题目大意:

    FJ同学去买东西,东西的价值为T,他和卖家都有N种金币,FJ希望交易完成时金币变化最小。

    求最少的金币变化数量。FJ的金币个数有限,奸商的金币数目无限。

解题思路:

    背包问题,FJ的每种金币个数有限可以看做是多重背包问题,奸商的金币数目无限可以看做是完全背包问题。

    设F1[i]为FJ付款为i时的最小金币数,设F2[i]为奸商找钱为i时的最小金币数。

    则F1[i+T]+F2[i]就是所求的最小金币变化数量。(F1用多重背包求解,F2用完全背包求解)

    PS:这里的背包求得是最小价值,且要恰好装满。故初始化数组时应 F[0]=0,F[1-MAXN]=INT_MAX;(好久没做背包了,下意识把F[1]=0了,结果T==1时总是输出0,查了好久。。。)

Code:

 #include<string>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<limits.h>
#define MAXN 1000000
#define INF 9999999 //背包被调 直接抄的背包九讲,因为有两个数组,增加一个数组参数
using namespace std;
int N,V,c[MAXN+],a[MAXN+],w=,f1[MAXN+],f2[MAXN+];
int min(int a,int b)
{
return a>b?b:a;
}
void ZeroOnePack(int cost,int weight,int f[]) //01背包
{
for (int v=V; v>=cost; v--)
f[v]=min(f[v],f[v-cost]+weight);
}
void CompletePack(int cost,int weight,int f[]) //完全背包
{
for (int v=cost;v<=V;v++)
f[v]=min(f[v],f[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount,int f[]) //多重背包
{
if (cost*amount>=V)
{
CompletePack(cost,weight,f);
return ;
}
int k=;
while (k<amount)
{
ZeroOnePack(k*cost,k*weight,f);
amount=amount-k;
k*=;
}
ZeroOnePack(amount*cost,amount*weight,f);
}
void init(int M,int f[])
{
f[]=; //保证背包装满 具体原因参见背包九讲
for (int i=; i<=M; i++) //求最小价值要把初值赋值为正无穷(INT_MAX可能会导致整型溢出)
f[i]=INF;
}
int main()
{
while (cin>>N>>V)
{ int V2=V;
int max=;
for (int i=; i<=N; i++){
cin>>c[i];
if (c[i]>max) max=c[i];}
for (int i=; i<=N; i++)
cin>>a[i];
V=max*max+V2+; //要找钱,V要比T大很多才行
init(V,f1);
init(V,f2);
for (int i=;i<=N;i++)
MultiplePack(c[i],,a[i],f1);
for (int i=;i<=N;i++)
CompletePack(c[i],,f2);
int ans=INF;
for (int i=;i<=V-V2;i++)
if (f1[i+V2]!=INF&&f2[i]!=INF) ans=min(ans,f1[i+V2]+f2[i]);
if (ans!=INF) printf("%d\n",ans); //ans==INF表示数据没有变过,则表示无解
else printf("-1\n");
}
return ;
}

POJ3260——The Fewest Coins(多重背包+完全背包)的更多相关文章

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

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

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

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

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

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

  4. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

  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. The Fewest Coins POJ - 3260

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

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

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

  8. POJ3260The Fewest Coins[背包]

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

  9. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

随机推荐

  1. 算法 replace,replace_copy,back_inserter

    replace (list.begin(), list.end(), , ); // replace any elements with value of 0 by 42 replace算法对输入序列 ...

  2. poj 1077 Eight(A*)

    经典的八数码问题,用来练习各种搜索=_=.这题我用的A*做的,A*的主要思想就是在广搜的时候加了一个估价函数,用来评估此状态距离最终状态的大概距离.这样就可以省下很多状态不用搜索.对于每个状态设置一个 ...

  3. L006-oldboy-mysql-dba-lesson06

    L006-oldboy-mysql-dba-lesson06 数据清理状态,先标记update table state=1,再删除. myisam没外键,硬件,并发,锁表力度,不支持事务,OLAP. ...

  4. struts2使用struts2-bootstrap-plugin插件

    1.下载插件 http://code.google.com/p/struts2-bootstrap/ 2.添加maven依赖 <dependency> <groupId>com ...

  5. 连续改变Chrome浏览器窗口大小,可以导致内存泄漏

    最近在做响应式布局的页面,在开发测试过程中,为了看到页面在不同尺寸的窗口中的表现,因此要不停的拖动浏览器来改变其窗口大小:开始在Chrome浏览器下查看页面,拖动了几次,感觉电脑明显的卡了下来,刚开没 ...

  6. 手写一个自己的简单MVC框架myPHP

    myPHP框架 采用的是MVC 思想,应用纯面向对象及项目单一入口,实现的一个自定义的框架.(自己兴趣的练习) 一.项目单一入口 入口文件 myphp\index.php前台 一个网站所有的请求都请求 ...

  7. Ubuntu 12.04 安装sougou for linux

    安装sougou for linux: 1.卸载原有的输入法,fcitx或ibus.如卸载fcitx: sudo apt-get remove fcitx*(如不需保留配置文件用purge) sudo ...

  8. opencv学习笔记(01)——操作图像的像素

    #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <ope ...

  9. MenuItem

    private void 文件ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("打开测试" ...

  10. 基于C#利用金山取词组件实现屏幕取词功能

    这个程序在网上有很多例子,近期要做的项目中有和这个有某些一点点相似的地方,就练练,发现在本机上(Win 7 64位)不能实现其功能,可能原因是API组件太老了吧,毕竟金山大佬公布他的组件是2005年, ...