這是我爲我的一個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]
一種方法是使用蠻力,讓所有組合,並找到滿足爲準解決方案,並找到一個具有最小。到目前爲止,這是我能想到的。 沒有假設兩個項目組合的價格會低於分別購買每個項目的價格。
任何建議,提示,歡迎
爲什麼每個組合有1個實際價格與每個項目相關聯? – DonkeyKong 2012-04-20 06:26:39