(混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
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
解法:支付时硬币数量有限制,为多重背包问题,通过二进制方法转化为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)的更多相关文章
- 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: ...
- 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种货币 ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- hdoj2191 珍惜现在,感恩生活(01背包 || 多重背包)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2191 思路 由于每种大米可能不止一袋,所以是多重背包问题,可以直接使用解决多重背包问题的方法,也可以将 ...
- dp--01背包,完全背包,多重背包
背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...
- 多重背包转化成完全背包 E - Charlie's Change
http://poj.org/problem?id=1787 这个题目我一看就觉得是一个多重背包,但是呢,我不知道怎么输出路径,所以无可奈何,我就只能看一下题解了. 看了题解发现居然是把多重背包转化成 ...
- nyoj 311-完全背包 (动态规划, 完全背包)
311-完全背包 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:5 submit:7 题目描述: 直接说题意,完全背包定义有N种物品和一个容量为V的 ...
随机推荐
- 书单.md
0823 John Hoskin, An Ilustrated History of Thailand.Asia Books Co., Ltd.2015 0729 Gerald Graff, Cath ...
- CSS3转换
一.2D转换 1.css3 rotate()旋转 通过指定的角度参数对原元素指定一个2D rotation(2D 旋转) 语法: transform:rotate(<angle>); ...
- Struts框架——(二)Struts原理with登录实例
二. Struts基本工作流程 假设现在有以下情景: 用户正在浏览一个用STRUTS的技术构建的网站主页,主页上有个登陆表单,用户填好登陆名和密码,单击"登陆"按钮,就激活了以下一 ...
- 检查项目里是否有IDFA的方法
检查项目里是否有IDFA的方法: 步骤:1.打开终端cd到要检查的文件的根目录. 2.执行下列语句:grep -r advertisingIdentifier . (别少了最后那个点号). 发现有ma ...
- JQuery Easy Ui DataGrid
Extend from $.fn.panel.defaults. Override defaults with $.fn.datagrid.defaults. The datagrid display ...
- C++ 中数串互转、进制转换的类
/******************************************************************** created: 2014/03/16 22:56 file ...
- Activity之间传递参数(一)
-------siwuxie095 传递简单数据 (1)首先创建一个项目:SendArgs (2)选择API:21 Android 5.0 (3)选择 Empty Activity (4)默认 (5) ...
- Hdu OJ 5965 扫雷(递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5965 题目大意:中文题,自己读 解图思路:对于每一列都有三种情况--0, 1, 2. 如果第一列确定地 ...
- php常用的数组函数
array_change_key_case -- 返回字符串键名全为小写或大写的数组 array_chunk -- 将一个数组分割成多个 array_combine -- 创建一个数组,用一个数组的 ...
- PLSQLDeveloper 提示不能初始化?
原因: oracle数据库是64位的,而 PLSQL Developer 只有32位的! 下载PLSQL_Developer地址: http://pan.baidu.com/share/link?sh ...