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

来源:互联网 发布:淘宝商品类目大全 编辑:程序博客网 时间:2024/05/28 06:04

问题描述和分析请查看王晓东编著的<<算法设计与分析>>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;            }        }       }}
0 0
原创粉丝点击