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.
 
题意:John去买东西,东西的价格是T(1 <= T <= 10000),John所在的地方有n(1 <= n <= 100)种的硬币,面值分别为V1, V2, ..., Vn (1 <= Vi <= 120)。John带了C1枚面值为V1的硬币,C2枚面值为V2的硬币,...,Cn枚面值为Vn的硬币(0 <= Ci <= 10000)。售货员那里每种硬币都有无限多个。问为了支付这个T,John给售货员的硬币数目加上售货员找回的零钱的硬币数目最少是多少。如果无法支付 T,输出-1 。

解法:支付时硬币数量有限制,为多重背包问题,通过二进制方法转化为01背包求解。找零时,硬币数量无限制,为完全背包问题。对两问题分别求解,然后找出差额为T时,两者和的最小值即为所示。

 
 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; #define met(a,b) (memset(a,b,sizeof(a)))
#define N 20000
#define INF 0x3f3f3f3f int V[], C[];
int v[N], c[N], dp[N];
int n, T, sum, k, Update; ///Update为更新的范围, 价值T最大为10000,而V[i]最大为120, 因此Update为10200便可以 void Init() ///将多重背包利用倍增法,转化为01背包
{ ///并解决一些初始化的问题
int i, j; k=;
for(i=; i<=n; i++)
{
for(j=; j<=C[i]; j*=)
{
v[k] = V[i]*j;
c[k++] = j;
C[i] -= j;
}
if(C[i])
{
v[k] = V[i]*C[i];
c[k++] = C[i];
C[i] = ;
}
}
k--; for(i=; i<=Update; i++)
dp[i] = INF;
} void First()
{
int i, j; dp[] = ;
for(i=; i<=k; i++)
{
for(j=Update; j>=v[i]; j--)
dp[j] = min(dp[j], dp[j-v[i]]+c[i]);
}
} int Secound()
{
int i, j; for(i=; i<=n; i++)
{
for(j=Update-V[i]; j>=; j--)
{///看好哦, 这里是加号,也就是从后面更新过来的, 于是第二重循环要逆着来
dp[j] = min(dp[j], dp[j+V[i]]+);
}
} return dp[T];
} int main()
{
while(scanf("%d%d", &n, &T)!=EOF)
{
int i;
sum=;
for(i=; i<=n; i++)
scanf("%d", &V[i]);
for(i=; i<=n; i++)
{
scanf("%d", &C[i]);
sum += V[i]*C[i];
}
Update=; Init(); ///初始化
First();///01背包
int ans = Secound(); ///完全背包 if(T>sum) printf("-1\n");
else
{
if(ans==INF) ///如果ans==INF说明并没有更新到dp[T],不能兑换到
printf("-1\n");
else
printf("%d\n", dp[T]);
} }
return ;
}

(混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)的更多相关文章

  1. The Fewest Coins POJ - 3260

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

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

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

  3. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

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

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

  5. POJ3260The Fewest Coins[背包]

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

  6. (多重背包+记录路径)Charlie's Change (poj 1787)

    http://poj.org/problem?id=1787   描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...

  7. hdoj2191 珍惜现在,感恩生活(01背包 || 多重背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2191 思路 由于每种大米可能不止一袋,所以是多重背包问题,可以直接使用解决多重背包问题的方法,也可以将 ...

  8. dp--01背包,完全背包,多重背包

    背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...

  9. 多重背包转化成完全背包 E - Charlie's Change

    http://poj.org/problem?id=1787 这个题目我一看就觉得是一个多重背包,但是呢,我不知道怎么输出路径,所以无可奈何,我就只能看一下题解了. 看了题解发现居然是把多重背包转化成 ...

  10. nyoj 311-完全背包 (动态规划, 完全背包)

    311-完全背包 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:5 submit:7 题目描述: 直接说题意,完全背包定义有N种物品和一个容量为V的 ...

随机推荐

  1. Tomasulo's Algorithm

    如果操作数在寄存器中,将会存在V字段,否则,设置Q字段来指示reservation station将会产生运算所需要的操作数 reorder buffer包含4个字段 the instruction ...

  2. 谈谈Activiti中流程对象之间的关系

    详细见:http://www.kafeitu.me/activiti/2012/03/22/workflow-activiti-action.html (咖啡兔好牛!) 详细见: http://blo ...

  3. [原创]AD9212采样方法

    随记 最近由于工程原因用到ADC的采样,选用了ADI公司的AD9212芯片,八通道10位ADC.在进行ADC的采样时,看到的想到的几种方法,在这里做个笔记记录一下. AD9212简介 详细说明可以在A ...

  4. Light OJ 1032 - Fast Bit Calculations(数学)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1032 题目大意:一个十进制数变化为二进制,那么对于这个数,如果连着两个二进制位 ...

  5. winAPI 检查系统设备拔插使用 WM_DEVICECHANGE 消息

    if(message->message == WM_DEVICECHANGE) { /*if (message->wParam == DBT_DEVICEARRIVAL || messag ...

  6. 中兴MF667S WCDMA猫Linux拨号笔记

    公司最近有个国外有个项目需要用到WCDMA猫,网上简单选型了一下决定使用ZTE的型号MF667S的猫,本以为在Linux下拨号是比较简单的(之前有两款3G猫的调试经验),估计半天能搞定,结果折腾了一周 ...

  7. 炼数成金(dataguru)IT技能修炼

    2016我定的目标就是要走出舒适区,进入学习区!为了少走弯路,节约学习的成本和时间,我选择了dataguru.看到心仪的课程毫不犹豫的就报了名. 分享了炼数成金邀请码,使用邀请码报名课程可以减免50% ...

  8. X64操作系统组件Jmail无法正常服务问题

    故障现象: 近日,在VMware虚拟化部署迁移中,之前物理服务器中部署网站ASP组件Jmail服务一切正常,迁移完成后发现Jmail无法正常工作,其余组件能正常工作. 环境:Windows Serve ...

  9. Robot Framework入门学习2 创建第一个测试用例

    本文章部分内容引自以下网址,感谢作者的辛苦分享 http://www.cnblogs.com/fnng/p/3871712.html http://blog.csdn.net/tulituqi/art ...

  10. 安装软件 报错1304 C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox\dbghelp.dll验证您对该目录具有访问权限

    现在情况是C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox这个文件夹里面没有dbghelp.dll文件,就需要在百度上面 ...