POJ_3260_The Fewest Coins

[url]http://poj.org/problem?id=3260[/url]

以下是从网上拷过来的分析:
[color=red][size=large]题意: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时,两者和的最小值即为所示。

分析:
1.给钱时,硬币数有限制,为多重背包问题
2.找钱时,硬币数无限制,为完全背包问题
3.给钱上界为:T+maxValue^2,其中maxValue为最大硬币面值
证明:反证法。假设存在一种支付方案,John给的钱超过T+maxValue^2
则售货员找零超过maxValue^2,则找的硬币数目超过maxValue个
将其看作一数列,求前n项和sum(n)
根据鸽巢原理,至少有两个对maxValue求模的值相等
假设为sum(i)和sum(j),i<j
则i+1j的硬币面值和为maxValue的倍数
同理,John给的钱中也有一定数量的硬币面值和为maxValue的倍数
则这两堆硬币可用数量更少的maxValue面值硬币代替,产生更优方案[/size][/color]

[size=medium]我的代码【背包九讲的风格】:[/size]


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;
#define inf 0x3fffffff

//需要有两个dp,一个是付钱【多重背包】,一个是找钱【完全背包】,2者必须独立求解
int dp[25000], dp2[25000], w[105], num[105], MAX, maxs;
//dp存放的值是硬币个数,dp数组的下标意义:钱的数量
//dp[j]:给j钱至少需要dp[j]个硬币,dp2[j]:找j钱至少需要dp2[j]个硬币

void _01pack (int cost, int weight)
{
int j;
for (j = MAX; j >= cost; j--) //取最小时由于初始化为负无穷,所以要讨论,下面同理
{
if (dp[j] >= 0 && dp[j-cost] >= 0)
dp[j] = min (dp[j], dp[j-cost] + weight);
else if (dp[j-cost] >= 0)
dp[j] = dp[j-cost] + weight;
}
/*cout << "-" << endl; //打表调试
for (j = 0; j <= 400; j++)
cout << dp[j] << ' ';
cout << endl;*/
}
void compack (int cost)
{
int j;
for (j = cost; j <= MAX; j++)
{
if (dp[j] >= 0 && dp[j-cost] >= 0)
dp[j] = min (dp[j], dp[j-cost] + 1);
else if (dp[j-cost] >= 0)
dp[j] = dp[j-cost] + 1;
}
/*cout << "--" << endl; //打表调试
for (j = 0; j <= MAX; j++)
cout << dp[j] << ' ';
cout << endl;*/
}
void mulpack (int cost, int weight, int amount) //付钱多重背包,包含上面2个函数
{
if (cost * amount >= MAX)
{
compack (cost);
return ;
}
int k = 1;
while (k <= amount)
{
_01pack (k*cost, k*weight); //注意,01背包必须得有weight变量,因为这里价值不一定是1了
amount -= k;
k <<= 1;
}
_01pack (amount*cost, amount*weight);
}
void compack2 (int cost) //找钱完全背包
{
int j;
for (j = cost; j <= maxs; j++)
{
if (dp2[j] >= 0 && dp2[j-cost] >= 0)
dp2[j] = min (dp2[j], dp2[j-cost] + 1);
else if (dp2[j-cost] >= 0)
dp2[j] = dp2[j-cost] + 1;
}
/*cout << "---" << endl;
for (j = 0; j <= 366; j++)
cout << dp2[j] << ' ';
cout << endl;*/
}
int main()
{
int n, t, i, mins, j;
while (cin >> n >> t)
{
maxs = 0;
for (i = 0; i < n; i++)
{
cin >> w[i];
if (maxs < w[i])
maxs = w[i];
}
maxs *= maxs; //找钱上界
MAX = t + maxs; //付钱上界
for (i = 0; i < n; i++)
cin >> num[i];
for (i = 1; i <= MAX; i++)
dp[i] = -inf;
dp[0] = 0; //dp初始化,付钱
for (i = 0; i < n; i++)
mulpack (w[i], 1, num[i]);
for (i = 1; i <= maxs; i++)
dp2[i] = -inf;
dp2[0] = 0; //dp2初始化,找钱
for (i = 0; i < n; i++)
compack2 (w[i]);
mins = inf;
for (j = t; j <= MAX; j++) //枚举所以付钱情况,枚举所用硬币数,找出最小
{
if (dp[j] >= 0 && dp2[j-t] >= 0)//检查是否可以付j钱,是否可以找j-t钱,买东西要花t钱
mins = min (mins, dp[j]+dp2[j-t]);
}
if (mins == inf) //发现没有合法情况
puts ("-1");
else cout << mins << endl;
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值