貪心演算法(上)
阿新 • • 發佈:2018-11-30
一、問題描述
有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