以下是对 this 的尝试解决方案SPOJ 问题。输入是:
- 一定数量硬币的总重量
- 使用货币的硬币的值(value)和相应的权重,目标是找到给定金额的最小可能货币值(value)。
我对 wikipedia article 中背包问题的动态规划解决方案进行了轻微修改关于背包问题——我只是首先按重量对硬币进行排序,这样我就不必遍历所有硬币来获得最小值,并且(希望)确保组合重量等于容量。 (请看代码,它真的很简单并且有注释。)
但是,根据法官的说法,我的算法给出了错误答案的输入。您能指出算法有什么问题吗?
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
typedef unsigned int weight_t;
typedef unsigned int value_t;
struct coin {
weight_t weight;
value_t value;
};
coin make_coin(weight_t weight, value_t value) {
coin ret;
ret.weight = weight;
ret.value = value;
return ret;
}
bool compare_by_weight(const coin& coin1, const coin& coin2) {
return coin1.weight < coin2.weight;
}
int main() {
unsigned int test_cases;
cin >> test_cases;
while(test_cases--) {
//Initialization
unsigned int number_of_coins, n = 0;
weight_t empty_pig, full_pig, coin_weight, coins_weight;
value_t coin_value, min_value, current_value = 0;
vector<coin> coins;
vector<unsigned int> min_value_for_the_weight;
//Input
cin >> empty_pig >> full_pig;
cin >> number_of_coins;
n = number_of_coins;
while(n--) {
cin >> coin_value >> coin_weight;
coins.push_back(make_coin(coin_weight, coin_value));
}
//Input processing
coins_weight = full_pig - empty_pig;
sort(coins.begin(), coins.end(), compare_by_weight);
min_value_for_the_weight.resize(coins_weight+1);
for(unsigned int i = 0; i < coins_weight; i++) min_value_for_the_weight[i] = 0;
//For all weights
for(unsigned int i = 1; i <= coins_weight; i++) {
//Find the smallest value
min_value = numeric_limits<value_t>::max();
for(unsigned int j = 0; j < number_of_coins; j++) {
//The examined coin weights more or same than examined total weight and we either already have put a coin
//in, or this is the first one
if(coins[j].weight <= i && (min_value_for_the_weight[i - coins[j].weight] > 0 || i == coins[j].weight)){
current_value = coins[j].value + min_value_for_the_weight[i - coins[j].weight];
if(current_value < min_value) min_value = current_value;
} else break; // <- this I deleted to get accepted
}
if(min_value == numeric_limits<value_t>::max()) min_value = 0;
min_value_for_the_weight[i] = min_value;
}
//If the piggy empty, output zero
if(!min_value_for_the_weight[coins_weight] && coins_weight != 0)
cout << "This is impossible." << endl;
else
cout << "The minimum amount of money in the piggy-bank is " << min_value_for_the_weight[coins_weight] << "." << endl;
}
return 0;
}
最佳答案
案例empty_pig == full_pig
是有问题的,因为你忽略了重复逻辑特例 min_value_for_the_weight
的第零个条目.
另一个错误是它只是一个好主意break
如果coins[j].weight > i
.旧代码可以 break
什么时候coin[j].weight <= i
并且合取的另一半是假的,即不可能用重量 i - coins[j].weight
来制造硬币.这发生在以下测试用例中。
1
10 13
2
2 2
3 3
我们必须使用重量为 2 或 3 的硬币来制作重量 3。重量 1 被正确地确定为不可能。权重 2 被正确确定为可能。对于权重3,我们尝试权重2的硬币,确定权重1是不可能的,break
在尝试重量为 3 的硬币之前。结果是代码错误地报告不可能使权重为 3。
关于c++ - 修改后的动态背包 - 有问题的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25557911/