算法_动态规划_01背包问题(重量为浮点型)问题描述和分析请查看王晓东编著的<<;算法设计与分析>>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> (1);
Point (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.(k+1));
list.(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 (0);
for(int i=1;i<list.size();i++){
(i).sumWeight>=pre.sumWeight&&(i).sumValue<pre.sumValue){
deleteList.(i));
平行线公理
}el{
(i);
}
}怪盗基德语录
for(int i=0;i<deleteList.size();i++){
<(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 (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);
Int();
Double();妙笔生花
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){
return1;
}el if(this.sumWeight<arg0.sumWeight){
return -1;
}el{
if(this.sumValue>arg0.sumValue){
return1;
}el if(this.sumWeight<arg0.sumWeight){
return -1;
}el{
return0;
}
}
}
}