• 使用STL处理分支限界法处理最优装载问题


    View Code
      1 #include <iostream>
      2 #include <vector>
      3 #include <queue>
      4 #include <time.h> 
      5 #define MAX_SIZE 100
      6 int SIZE;
      7 using namespace std;
      8 float Object_Weight[MAX_SIZE];
      9 float SUM;
     10 class Node{
     11 public:
     12     float total_weight;    
     13     int level;
     14     Node(){
     15         total_weight = 0;
     16         level = 0;
     17         for(int i=0;i<SIZE;i++)
     18             result[i] = false;
     19     }
     20     Node(const Node& obj){ 
     21         total_weight = obj.total_weight;
     22         level = obj.level;
     23         for(int i=0;i<SIZE;i++)
     24             result[i] = obj.result[i];
     25     }
     26     Node& operator = (const Node &obj){
     27         total_weight = obj.total_weight;
     28         level = obj.level;
     29         for(int i=0;i<SIZE;i++)
     30             result[i] = obj.result[i];
     31         return *this;
     32     }
     33     void set(bool value){ 
     34         result[level-1] = value;    
     35         total_weight = getWeight();
     36     }
     37     float returnWeight(){return total_weight;}
     38     float maxEstWeight();
     39     
     40     void CopyResult(bool* re);
     41 private:
     42     float getWeight();    
     43     bool result[MAX_SIZE];
     44 };
     45 struct cmp{
     46     bool operator()(Node& obj1, Node& obj2){
     47         return obj1.total_weight<obj2.total_weight;
     48     }
     49 };
     50 void Node::CopyResult(bool* re){
     51     for(int i=0;i<SIZE;i++)
     52         re[i] = result[i];
     53 }
     54 float Node::getWeight(){
     55     float sum = 0;
     56     for(int i=0;i<level;i++)
     57     {
     58         if(result[i])
     59             sum += Object_Weight[i]; 
     60     }
     61     return sum;
     62 }
     63 
     64 float Node::maxEstWeight(){
     65     float sum = total_weight;
     66     for(int i=level;i<SIZE;i++)
     67         sum += Object_Weight[i];
     68     return sum;
     69 }
     70 void naiveMethod(float c1,float c2){
     71     float bestWeight = 0;
     72     int counter = 0;
     73     bool* bestResult = new bool(SIZE);
     74     vector<Node> Queue;
     75     Node *a = new Node();
     76     Queue.push_back(*a);
     77     while(Queue.size() != 0){
     78         Node temp(Queue[0]);
     79         Queue.erase(Queue.begin());
     80         if(temp.level != SIZE){
     81             Node left(temp);
     82             Node right(temp);
     83             left.level++;
     84             left.set(false);
     85             right.level++;
     86             right.set(true);
     87             Queue.push_back(left);
     88             Queue.push_back(right);
     89             counter += 2;
     90         }
     91         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
     92             bestWeight = temp.returnWeight();
     93             temp.CopyResult(bestResult);
     94         }
     95     }//while
     96     cout<<"c1 loading result:"<<bestWeight<<endl;
     97     for(int i=0;i<SIZE;i++)
     98         cout<< bestResult[i]<<"    ";
     99     cout<<endl;
    100     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    101     for(int i=0;i<SIZE;i++)
    102         cout<<! bestResult[i]<<"    ";
    103     cout<<endl;
    104     cout<<"Total counter:    "<<counter<<endl;
    105 }
    106 
    107 void queueMethod(int c1, int c2){
    108     float bestWeight = 0;
    109     int counter = 0;
    110     bool* bestResult = new bool(SIZE);
    111     vector<Node> Queue;
    112     Node *a = new Node();
    113     Queue.push_back(*a);
    114     while(Queue.size() != 0){
    115         Node temp(Queue[0]);
    116         Queue.erase(Queue.begin());
    117         if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
    118             Node left(temp);
    119             Node right(temp);
    120             left.level++;
    121             left.set(false);
    122             right.level++;
    123             right.set(true);
    124             Queue.push_back(left);
    125             Queue.push_back(right);
    126             counter += 2;
    127         }
    128         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
    129             bestWeight = temp.returnWeight();
    130             temp.CopyResult(bestResult);
    131         }
    132     }//while
    133     cout<<"c1 loading result:"<<bestWeight<<endl;
    134     for(int i=0;i<SIZE;i++)
    135         cout<< bestResult[i]<<"    ";
    136     cout<<endl;
    137     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    138     for(int i=0;i<SIZE;i++)
    139         cout<<! bestResult[i]<<"    ";
    140     cout<<endl;
    141     cout<<"Total counter:    "<<counter<<endl;
    142 }
    143 
    144 
    145 void priority_QueueMethod(int c1, int c2){
    146     float bestWeight = 0;
    147     int counter = 0;
    148     bool* bestResult = new bool(SIZE);
    149     priority_queue<Node, vector<Node>, cmp> Queue;
    150     Node *a = new Node();
    151     Queue.push(*a);
    152     while(Queue.size() != 0){
    153         Node temp(Queue.top());
    154         Queue.pop();
    155         if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
    156             Node left(temp);
    157             Node right(temp);
    158             left.level++;
    159             left.set(false);
    160             right.level++;
    161             right.set(true);
    162             Queue.push(left);
    163             Queue.push(right);
    164             counter += 2;
    165         }
    166         if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
    167             bestWeight = temp.returnWeight();
    168             temp.CopyResult(bestResult);
    169         }
    170     }//while
    171     cout<<"c1 loading result:"<<bestWeight<<endl;
    172     for(int i=0;i<SIZE;i++)
    173         cout<< bestResult[i]<<"    ";
    174     cout<<endl;
    175     cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
    176     for(int i=0;i<SIZE;i++)
    177         cout<<! bestResult[i]<<"    ";
    178     cout<<endl;
    179     cout<<"Total counter:    "<<counter<<endl;
    180 }
    181 
    182 int main(){
    183     float c1,c2;
    184     SUM= 0;
    185     cout<<"SIZE:"<<endl;
    186     cin>>SIZE;
    187     cout<<"WEIGHT:"<<endl;
    188     for(int i=0;i<SIZE;i++){
    189         cin>>Object_Weight[i];
    190         SUM += Object_Weight[i];
    191     }
    192     cout<<"C1:"<<endl;
    193     cin>>c1;
    194     cout<<"C2:"<<endl;
    195     cin>>c2;
    196     if(c1+c2<SUM)
    197     {
    198         cout<<"No solution!"<<endl;
    199         return EXIT_SUCCESS;
    200     }
    201     if(SUM<c1 || SUM<c2)
    202     {
    203         cout<<"Need only one ship!"<<endl;
    204         return EXIT_SUCCESS;
    205     }
    206     time_t start ,end ;  
    207     double cost;  
    208     start = clock();  
    209     naiveMethod(c1, c2);
    210     end = clock();  
    211     cost=difftime(end,start);  
    212     cout<<"///////////////
    Naive method time:    "<<cost<<"
    ///////////////"<<endl;
    213     start = clock();  
    214     queueMethod(c1,c2);
    215     end = clock();  
    216     cost=difftime(end,start);  
    217     cout<<"///////////////
    Queue method time:    "<<cost<<"
    ///////////////"<<endl;
    218     start = clock();  
    219     priority_QueueMethod(c1,c2);
    220     end = clock();  
    221     cost=difftime(end,start);  
    222     cout<<"///////////////
    Priority queue method time:    "<<cost<<"
    ///////////////"<<endl;
    223     return EXIT_SUCCESS;
    224 }
  • 相关阅读:
    loj6033.「雅礼集训 2017 Day2」棋盘游戏
    loj6032. 「雅礼集训 2017 Day2」水箱
    BZOJ 5217 [Lydsy2017省队十连测] 航海舰队
    P4173 残缺的字符串
    P3723 [AH2017/HNOI2017]礼物
    P3321 [SDOI2015]序列统计
    P4841 [集训队作业2013]城市规划
    MySQL基础
    MySQL查询
    HTTP响应码
  • 原文地址:https://www.cnblogs.com/wangbiaoneu/p/OptLoading.html
Copyright © 2020-2023  润新知