01背包问题(DFS解法)

40 篇文章 3 订阅
9 篇文章 0 订阅

有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<Person> treeSet;
    static ArrayList<Person> 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;i<size;i++) {
                    System.out.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<Person> {
    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;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值