最小重量机器设计问题 java实现

来源:互联网 发布:ybi金融互助平台源码 编辑:程序博客网 时间:2024/06/08 18:33

暂时保存,有待改进

Code:
  1. import java.util.*;   
  2. public class TestMachine {   
  3.     public static void main(String[] args) {   
  4.         MinMachine mm = new MinMachine();   //最小重量机的对象   
  5.         mm.backTrack(0);                    //计算所有要得到的值   
  6.         System.out.println(mm.getBestW());  //打印最小重量   
  7.         int[] x = mm.getBestX();   
  8.         for(int i=0; i<x.length; i++) {     //输出最优解   
  9.             System.out.print(x[i]+" ");   
  10.         }   
  11.     }   
  12. }   
  13.   
  14. class MinMachine {   
  15.     int parts;                      //机器部件数   
  16.     int suppliers;                  //供应商数目   
  17.     float[][] price;            //机器的价格   
  18.     float[][] weight;           //机器的重量   
  19.     float cost;                 //限制的费用   
  20.     float currentCost;          //当前的花费   
  21.     float currentWeight;        //当前的重量   
  22.     static float bestW;             //当前最小重量   
  23.     static int bestX[];       //最优解   
  24.     static int saveX[];             //保存记录   
  25.        
  26.     public MinMachine() {   
  27.         bestW = 0;              //开始或者限制费用太小结果为0   
  28.         currentWeight = 0;   
  29.         currentCost = 0;   
  30.         Scanner sc = new Scanner(System.in);   
  31.         cost = sc.nextFloat();                     //输入要限制的费用   
  32.         parts = sc.nextInt();                      //为机器部件数赋值   
  33.         suppliers = sc.nextInt();                                    //为供应商数赋值   
  34.         price = new float[parts][suppliers];          
  35.         weight = new float[parts][suppliers];         
  36.         for(int i=0; i<parts; i++) {   
  37.             for(int j=0; j<suppliers; j++) {   
  38.                 price[i][j] = sc.nextFloat();          //机器部件的价格   
  39.                 weight[i][j] = sc.nextFloat();               //机器部件的重量   
  40.             }   
  41.         }   
  42.         bestX = new int[parts];   
  43.         saveX = new int[parts];   
  44.            
  45.         for(int k=0; k<parts; k++) {   
  46.             bestX[k] = -1;   
  47.         }   
  48.     }   
  49.        
  50.     void backTrack(int i) {   
  51.         if(i >= parts) {                           //达到叶子节点   
  52.             if(currentWeight<bestW || bestW==0) {    /*当前能找到的最小重量<以前最小重量  
  53.                                                          保留当前的最小重量*/  
  54.                 bestW = currentWeight;   
  55.         for(int k=0; k<parts; k++) {           //把当前搜过的路径记下来   
  56.           saveX[k] = bestX[k];   
  57.         }   
  58.       }    
  59.       return;   
  60.     }   
  61.     for(int j=0; j<suppliers; j++) {           //依次递归尝试这些供应商   
  62.         if((currentCost+price[i][j]<cost) && (weight[i][j]>0)) {   
  63.             currentCost += price[i][j];   
  64.             currentWeight += weight[i][j];   
  65.             bestX[i] = j;   
  66.         backTrack(i+1);   
  67.         bestX[i] = -1;   
  68.         currentCost -= price[i][j];   
  69.             currentWeight -= weight[i][j];   
  70.       }   
  71.     }   
  72.     }   
  73.        
  74.     float getBestW() {      //获取最优重量   
  75.         return bestW;   
  76.     }   
  77.        
  78.     int[] getBestX() {      //获取最优解   
  79.         return bestX;   
  80.     }   
  81.        
  82.     int[] getSaveX() {      //获取记录的解   
  83.         return saveX;   
  84.     }   
  85. }  

 

原创粉丝点击