这是我为我的一个 CS 课设置的问题,我有点卡住了。这是问题的摘要。
Create a program that will:
1) take a list of grocery stores and its available items and prices
2) take a list of required items that you need to buy
3) output a supermarket where you can get all your items with the cheapest price
input: supermarkets.list, [tomato, orange, turnip]
output: supermarket_1
The list looks something like
supermarket_1
$2.00 tomato
$3.00 orange
$4.00 tomato, orange, turnip
supermarket_2
$3.00 tomato
$2.00 orange
$3.00 turnip
$15.00 tomato, orange, turnip
If we want to buy tomato and orange, then the optimal solution would be buying
from supermarket_1 $4.00. Note that it is possible for an item to be bough
twice. So if wanted to buy 2 tomatoes, buying from supermarket_1 would be the
optimal solution.
到目前为止,我已经能够将数据集放入一个数据结构中,希望能让我轻松地对其进行操作。我基本上有一个超市字典,该值将指向另一个字典,其中包含从每个条目到其价格的映射。
supermarket_1 --> [turnip --> $2.00]
[orange --> $1.50]
一种方法是使用蛮力,获取所有组合并找到满足解决方案的任何一个并找到最小的那个。到目前为止,这是我能想到的。没有假设两种物品组合的价格会低于单独购买的价格。
欢迎任何建议提示