当前位置 博文首页 > 用心编码:贪心算法(上)

    用心编码:贪心算法(上)

    作者:[db:作者] 时间:2021-09-07 13:29

    一、问题描述
    有15个公司和30种产品。
    每个公司生产不同种类任意多种产品,每个公司生成产品都会产生一定的费用。
    例如:

    需要生产6种产品,分别为 1 5 9 10 11 12
    6 1 5 9 10 11 12
    共有7个公司生产
    7
    第一家:需要15万,生成5种产品,1 3 5 7 9
    15 5 1 3 5 7 9
    第二家...
    12 6 2 3 4 6 7 8
    第三家...
    7 5 3 4 5 6 7
    第四家...
    16 4 8 9 10 11
    第五家...
    5 1 2
    第六家...
    33 12 1 2 3 4 5 6 7 8 9 10 11 12
    第七家...
    9 3 1 12 13

    问:选择那几家公司来生产产品,价格最少?

    二、代码(dfs算法)

    package com.daxiong.day9;
    
    import java.util.Scanner;
    import java.io.*;
    
    public class DFSFactory {
    
        private static int needSize = 30;
        private static int[] needData = new int[needSize];
        private static int facSize = 15;
        private static Factory[] facList = new Factory[facSize];
    
        private static int totalCost = 10000;
        private static int[] stack = new int[needSize + 1];
        private static int stackIndex = 1;
        private static int stackFlag = 1;
    
        public static void main(String[] args) throws Exception {
            readData();
            print();
            System.out.println();
            dfs(0);
        }
    
        public static void dfs(int index) {
    
            if (index == facSize) {
                return;
            }
    
            Factory initFac = facList[index];
            int[] initCates = initFac.categories;
            stack[0] = initFac.cost; // 首位存钱
            for (int i = 0; i < needSize; i++) {
                for (int j = 0; j < initFac.size; j++) {
                    if (needData[i] == initCates[j]) {
                        stack[stackIndex] = needData[i];
                        stackIndex++;
                    }
                }
            }
            System.out.println("factory:" + index + "," + initFac.cost);
            for (int i = index + 1; i < facSize; i++) {
                Factory factory = facList[i];
                if (isOk(factory, i) == 0 && factory.selected == 0) {
                    System.out.println("花费:" + (stack[0] < totalCost ? stack[0] : totalCost)); // 清空当前
                    /*System.out.println("stackFlag:" + stackFlag);
                    System.out.println("stackIndex:" + stackIndex);*/
    
                    for (int j = stackFlag; j < stackIndex; j++) {
                        stack[j] = 0;
                    }
                    stack[0] = stack[0] - factory.cost;
                    stackIndex = stackFlag;
                    factory.selected = 1;
                    System.out.println();
                }
            }
    
            //  清空
            for (int j = 0; j < stackIndex; j++) {
                stack[j] = 0;
            }
            stackIndex = 1;
            stackFlag = 1;
            for (int j = 0; j < facSize; j++) {
                facList[j].selected = 0;
            }
            dfs(index + 1);
        }
    
        /**
         * @param nextFac
         * @return 0 : 满足条件! -1 : 不满足条件
         */
        public static int isOk(Factory nextFac, int facFlag) {
            stackFlag = stackIndex;
            int[] nextCates = nextFac.categories;
            boolean flagNeed;
            boolean flagNext;
            // 累积类别
            for (int i = 0; i < needSize; i++) {
                flagNeed = false;
                flagNext = false;
                for (int j = 1; j < stackIndex; j++) {
                    if (needData[i] == stack[j]) {
                        flagNeed = true;
                    }
                }
                // System.out.println("needData[" + i + "]:" + needData[i] +
                // ",flagNeed:" + flagNeed);
                if (!flagNeed) { // 说明没有
                    for (int j = 0; j < nextFac.size; j++) {
                        if (needData[i] == nextCates[j]) {
                            flagNext = true;
                        }
                    }
                }
                if (flagNext) {
                    stack[stackIndex] = needData[i];
                    stackIndex++;
                    // System.out.println("stackIndex:" + stackIndex);
                }
            }
    
            if (stackFlag != stackIndex) { // 有部分
                System.out.println("factory:" + facFlag + "," + nextFac.cost);
                stack[0] = stack[0] + nextFac.cost; // 存钱
            }
    
            if (stackIndex == (needSize + 1)) {
                return 0;
            } else {
                return -1;
            }
        }
    
        // 判断是否满足条件
    
        // 读取数据
        public static void readData() throws Exception {
            // Scanner sc = new Scanner(System.in);
            Scanner sc = new Scanner(new File("src/resource/case.txt"));
            while (true) {
                needSize = sc.nextInt();
                for (int i = 0; i < needSize; i++) {
                    needData[i] = sc.nextInt();
                }
                facSize = sc.nextInt();
                for (int i = 0; i < facSize; i++) {
                    int cost = sc.nextInt();
                    int size = sc.nextInt();
                    int[] categories = new int[size];
                    for (int j = 0; j < size; j++) {
                        categories[j] = sc.nextInt();
                    }
                    Factory fac = new Factory(cost, size, categories);
                    facList[i] = fac;
                }
                if (sc.nextInt() == 0) {
                    sc.close();
                    break;
                }
            }
        }
    
        // 测试数据
        public static void print() {
            for (int i = 0; i < facSize; i++) {
                System.out.print(facList[i].cost + " " + facList[i].size + " ");
                for (int j = 0; j < facList[i].size; j++) {
                    System.out.print(facList[i].categories[j] + " ");
                }
                System.out.println();
            }
        }
    
    }
    
    class Factory {
        int cost;
        int size;
        int[] categories;
        int selected;
    
        public Factory() {
    
        }
    
        public Factory(int cost, int size, int[] categories) {
            super();
            this.cost = cost;
            this.size = size;
            this.categories = new int[size];
            this.categories = categories;
            this.selected = 0;
        }
    }

    测试数据:

    6 1 5 9 10 11 12
    7
    15 5 1 3 5 7 9
    12 6 2 3 4 6 7 8
    7 5 3 4 5 6 7
    16 4 8 9 10 11
    5 1 2
    33 12 1 2 3 4 5 6 7 8 9 10 11 12
    9 3 1 12 13
    0
    cs
    下一篇:没有了