01背包问题(DFS解法)

有5个物体,每个物品只有一个,其重量分别是为2,2,6,5,4,价值分别为6,3,5,4,6,背包的载重量为10,求装入背包的物体及总质量。

计算结果:15

package com.lanQiaoFor6;

import java.util.ArrayList;
import java.util.TreeSet;

public class JAVA_6 {

    static TreeSet treeSet;
    static ArrayList arrayList;
    static Person[] persons;
    static Person[] realPersons;
    static int asnprice = 0;
    static int realAsnprice = 0;
    static int weightSum = 10;
    static int count = 0;
    static int size = 0;

    public static void main(String[] args) {
        persons = new Person[5];
        treeSet = new TreeSet<>();
        treeSet.add(new Person(2, 6));
        treeSet.add(new Person(2, 3));
        treeSet.add(new Person(6, 5));
        treeSet.add(new Person(5, 4));
        treeSet.add(new Person(4, 6));
        arrayList = new ArrayList<>();
        arrayList.addAll(treeSet);
        for (int i = 0; i < 5; i++) {
            Person person = arrayList.get(i);
            if (person.weight > weightSum) {
                continue;
            }
            person.flag = true;
            count += person.weight;
            asnprice += person.value;
            persons[size++] = person;
            if (i == 4) {
                if (person.value > realAsnprice) {
                    realAsnprice = (int) person.value;
                }
                continue;
            }
            dfs(arrayList.get(i + 1), count);
            person.flag = false;
            count -= person.weight;
            asnprice -= person.value;
            persons[size--] = null;
        }

        System.out.println(realAsnprice);

    }

    public static void dfs(Person person, int count) {
        // TODO Auto-generated method stub

        // 出口

        if (weightSum < count + person.weight) {
            if (asnprice > realAsnprice) {
                realAsnprice = asnprice;
                realPersons = persons.clone();
                for(int i=0;iout.print (persons[i].value+" ");
                }
                System.out.println();
            }
        }

        // 遍历
        for (int i = 0; i < 4; i++) {

            if (weightSum > count + person.weight && person.flag != true) {
                person.flag = true;
                count += person.weight;
                asnprice += person.value;
                persons[size++] = person;
                int index = arrayList.indexOf(person);
                if (index != arrayList.size()) {
                    dfs(arrayList.get(index + 1), count);
                    person.flag = false;
                    count -= person.weight;
                    asnprice -= person.value;
                    persons[size--] = null;
                }
            }

        }

    }
}

class Person implements Comparable {
    public double value;
    public double weight;
    public boolean flag = false;

    public Person(double weight, double value) {
        // TODO Auto-generated constructor stub
        this.value = value;
        this.weight = weight;
    }

    public int compareTo(Person o) {
        // TODO Auto-generated method stub
        if ((this.value / this.weight) > (o.value / o.weight)) {
            return -1;
        } else {
            return 1;
        }
    }

}

你可能感兴趣的:(-----ACM水题-----,贪心,DFS,ACM算法入门)