最优装载

【问题】

有一批集装箱要装上一艘载重量为c的轮船。其中集装箱i的重量为Wi。
要求:确定在装载体积不受限制的情况下,将尽可能多的集装箱装上轮船。

【代码】

template<class Type>  
void Loading(int x[],Type w[], Type c, int n)  
{  
    int *t = new int [n+1]; //存储排完序后w[]的原始索引  
    SelectSort(w, t, n);  
  
    for(int i=1; i<=n; i++)  
    {  
        x[i] = 0;//初始化数组x[]  
    }  
    for(int i=1; i<=n && w[t[i]]<=c; i++)  
    {  
        x[t[i]] = 1;  
        c -= w[t[i]];  
    }  
} 

 

posted @ 2017-04-11 16:35  王勋广  阅读(179)  评论(0编辑  收藏  举报