Dijkstra's ++
有個簡化版, 不過有一個問題 :)- orderItemList = {a, b, c, .... }
- ruleList = { (a,b):10, (a,c):14, (a):8, (b):8, (c):10, .... }
- // rule list sorting:
- // (1) number of items, desc; (2) price, asc
- // this should give customer the overall best price ... ?
- price = 0
- while ( orderItemList.notEmpty() ) {
- matchIdx = findFirstMatch(ruleList, orderItemList); // traverse ruleList, find the entry where all keys are in orderItemList
- rule = ruleList[matchIdx]
- price += rule.price();
- orderIList.removeAll(rule.items())
- ruleUsed.add(rule)
- }
複製代碼 有機會因為價錢O既安排, (a,b) 同 (a,c) o既價錢一樣, 但係 c 貴過 b, 咁樣野話, (a,b) + c 就會貴過 (a,c) + b; 如果要做到一定得, 咁就要做少少 tracking, effectively 變左同 dijkstra 一樣. 視乎你個 typical order list typically 有幾長, 有機會好大食.. |