算法_动态规划_01背包问题(重量为浮点型)

本文探讨了Java中算法优化技巧与数据结构的应用,通过实例解析如何提高程序效率并解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述和分析请查看王晓东编著的<<算法设计与分析>>P77

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    private static int n;
    private static double c;
    private static double[] weightArr;
    private static double[] valueArr;
    private static HashMap<Integer,ArrayList<Point>> p=new HashMap<Integer,ArrayList<Point>>();
    private static HashMap<Integer,ArrayList<Point>> q=new HashMap<Integer,ArrayList<Point>>();

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        init();
        fun();
        ArrayList<Point> list=p.get(1);
        Point p=list.get(list.size()-1);
        System.out.println(p.sumValue);
    }

    private static void fun(){
        ArrayList<Point> tempList=new ArrayList<Point>();
        tempList.add(new Point(0,0));
        p.put(n+1,tempList);
        calculateQ(tempList,n+1);
        for(int k=n;k>=1;k--){
            ArrayList<Point> list=new ArrayList<Point>();
            list.addAll(p.get(k+1));
            list.addAll(q.get(k+1));
            Collections.sort(list);
            clearSome(list);
            p.put(k,list);
            if(k!=1){
                calculateQ(list,k);
            }
        }
    }

    private static void clearSome(ArrayList<Point> list){
        ArrayList<Point> deleteList=new ArrayList<Point>();
        Point pre=list.get(0);
        for(int i=1;i<list.size();i++){
            if(list.get(i).sumWeight>=pre.sumWeight&&list.get(i).sumValue<pre.sumValue){
                deleteList.add(list.get(i));
            }else{
                pre=list.get(i);
            }

        }
        for(int i=0;i<deleteList.size();i++){
            list.remove(deleteList.get(i));
        }
    }

    private static void calculateQ(ArrayList<Point> list,int k){
        ArrayList<Point> tempList=new ArrayList<Point>();
        for(int i=0;i<list.size();i++){
            Point t=list.get(i);
            if(t.sumWeight+weightArr[k-1]<=c){
                Point newPoint=new Point(t.sumWeight+weightArr[k-1],t.sumValue+valueArr[k-1]);
                tempList.add(newPoint);
            }
        }
        q.put(k,tempList);
    }

    private static void init(){
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        c=sc.nextDouble();
        weightArr=new double[n+1];
        valueArr=new double[n+1];
        for(int i=1;i<=n;i++){
            weightArr[i]=sc.nextDouble();
        }   
        for(int i=1;i<=n;i++){
            valueArr[i]=sc.nextDouble();
        }
    }

}

class Point implements Comparable<Point>{
    double sumWeight;
    double sumValue;
    public Point(double sumWeight,double sumValue){
        this.sumWeight=sumWeight;
        this.sumValue=sumValue;
    }

    public int compareTo(Point arg0) {
        // TODO Auto-generated method stub
        if(this.sumWeight>arg0.sumWeight){
            return 1;
        }else if(this.sumWeight<arg0.sumWeight){
            return -1;
        }else{
            if(this.sumValue>arg0.sumValue){
                return 1;
            }else if(this.sumWeight<arg0.sumWeight){
                return -1;
            }else{
                return 0;
            }
        }   
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值