Selecting A combination of minimum cost

前端 未结 5 1630
天命终不由人
天命终不由人 2020-12-10 09:07

I have data of different items in a different restaurants

    Rest    Item     Price
    ----------------------
    ABC     dosa      14
    ABC     idly             


        
5条回答
  •  春和景丽
    2020-12-10 09:58

    try

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class Mult {
        /**
         * @param args
         */
        public static void main(String[] args) {
            Map> xx = new HashMap>();
            xx.put("ABC",new ArrayList());
            xx.get("ABC").add(new X("", 0));
            xx.get("ABC").add(new X("dosa", 14));
            xx.get("ABC").add(new X("idly", 30));
            xx.get("ABC").add(new X("idly+upma", 25));
    
    
            xx.put("123",new ArrayList());
            xx.get("123").add(new X("", 0));
            xx.get("123").add(new X("dosa", 30));
            xx.get("123").add(new X("idly", 7));
            xx.get("123").add(new X("upma", 12));
    
    
            xx.put("XYZ",new ArrayList());
            xx.get("XYZ").add(new X("", 0));
            xx.get("XYZ").add(new X("dosa", 20));
            xx.get("XYZ").add(new X("idly", 12));
            xx.get("XYZ").add(new X("upma", 20));
            xx.get("XYZ").add(new X("dosa+upma", 30));
            xx.get("XYZ").add(new X("dosa+idly+upma", 40));
    
            String[] t = {
                    "dosaidlyupma",
                    "idlydosaupma",
                    "upmaidlydosa",
                    "dosaupmaidly",
                    "upmadosaidly",
                    "idlyupmadosa"};
            Set targets = new HashSet(Arrays.asList(t));
    
            Map best = new HashMap();
    
            for(String restaurant:xx.keySet()){
                best.put(restaurant, Integer.MAX_VALUE);
                String combo = null;
                for(X a:xx.get(restaurant)){
                    int deal = a.price;
                    combo = a.item;
                    for(X b:xx.get(restaurant)){
                        deal = deal + b.price;
                        combo = combo + "+" + b.item;
                        for(X c:xx.get(restaurant)){
                            deal = deal + c.price;
                            combo = combo + "+" + c.item;
                            if (targets.contains(combo.replaceAll("\\+", ""))){
    //                          System.out.println(restaurant+"\t"+combo.replaceAll("\\+", "")+"\t"+deal);
                                if (best.get(restaurant) > deal){
                                    best.put(restaurant, deal);                 
                                }
                            }
                        }
                    }
                }
            }
    
            System.out.println(best);
        }
    
    }
    

    will give you

    {XYZ=40, ABC=39, 123=49}

    it's the old good brute force approach.

    not the best one, but for this small set, it works.

提交回复
热议问题