POJ3260——The Fewest Coins(多重背包+完全背包)
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(多重背包+完全背包)的更多相关文章
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ3260:The Fewest Coins(混合背包)
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- POJ3260 The Fewest Coins(混合背包)
支付对应的是多重背包问题,找零对应完全背包问题. 难点在于找上限T+maxv*maxv,可以用鸽笼原理证明,实在想不到就开一个尽量大的数组. 1 #include <map> 2 #inc ...
- POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)
Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...
- poj3260 The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he a ...
- The Fewest Coins POJ - 3260
The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...
- POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)
题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
随机推荐
- md5值计算
1.md5(Message Digest 5th/消息概要加密算法 第5版) REFER: MD5 On wikipedia 2.应用范围 ① 验证下载文件的完整性 ② 3.关于MD5的几个问题 ①只 ...
- VS2005上一个坑:关于pch 的 error C1023
昨天编译就报错: c1xx : fatal error C1023: ‘UnicodeDebug\ImEngine.pch’ : unexpected error with pch, try rebu ...
- 实现cookie跨域访问
需求:A系统(www.a.com)里设置一个浏览器cookie,B系统(www.b.com)需要能够访问到A设置的cookie. 通过HTML SCRIPT标签跨域写cookie: 由于html的sc ...
- YII框架源码分析(百度PHP大牛创作-原版-无广告无水印)
YII 框架源码分析 百度联盟事业部——黄银锋 目 录 1. 引言 3 1.1.Yii 简介 3 1.2.本文内容与结构 3 2.组件化与模块化 4 2.1.框架加载和运行流程 4 ...
- Review PHP设计模式之——观测模式
观测模式: <?php class car implements SplSubject{ private $carName; //车的类型 private $carState=0; //车的状态 ...
- 在AWS上安装laravel框架
博客已经迁移到www.imyzf.com,本站不再更新,请谅解! Laravel是现在非常热门的PHP框架,这几天我试着在亚马逊AWS的服务器上安装Laravel,遇到很多问题,最后还是成功了.我的系 ...
- php入门变量之字符串
字符串只是一块用引号括起来的字符:字母.数字.空格.标点符号,等等. 下面列出的全都是字符串: 'Huige' "In watermelon sugar" '100' 'Augus ...
- liger grid loadData
function fn_Search() { var beginDt = $("#txtBegin").val(); var endDt = $("#txtEnd&quo ...
- WPF多线程演示
WPF中的几种处理线程的工作方式: 1.简单的DispatcherTimer类似Timer控件 2.需要处理UI同步时,Dispatcher DispatcherOpertion 3.增强的Threa ...
- 因程序问题引起的服务器CPU负荷一直保持在90%以上
昨天早上刚到办公室,就接到客户的电话说其某台小型机的CPU负荷一直保持在90以上,告警短信发个不停,一直没有间断过.该服务器是一台IBM的小型机,性能应该还是不错的,出现这样的情况确实不太正常.登陆上 ...