代码之家  ›  专栏  ›  技术社区  ›  user2693928

有趣的算法任务电梯

  •  2
  • user2693928  · 技术社区  · 7 年前

    它类似于背包问题,但更复杂。

    从A到B有电梯。1次旅行的价格如下:

    1)如果只有一个人-身高单位为美分-如果是180->180美分

    2)如果超过一人->最高身高:[180、150、185]->共185美分。

    电梯的极限为N kg,大于或等于700-例如 700公斤。

    您有N个体重和身高的客户,例如:

    [{h:180,w:70},{h:180,w:60},…]

    任务是计算将所有客户机从A传输到B的最小成本。

    到目前为止我的解决方案是:

    我得到了可用的组合。

    如果我有5个客户:[1],[2]…[5],[1,2],[2,3]…[1,2,3]…,[1,2,3,4,5]

    现在的问题是我有255个组合(n个人)。

    我想我可以让所有的组合计算出最低价格,检查每次旅行的公斤数是否不超过最大公斤数,然后按如下方式返回:

    每个嵌套数组都是一次旅行中的人

    [[1],[2],[3],[4],[5]]-每人单独旅行 [[1],[2,3,4,5]-一个组合

    [[1],[2],[3,4,5]]-秒 [[1],[2],[3],[4],[5]]

    然后计算每一行,然后简单排序并返回第一行。

    对于5个客户端,这是可行的,但对于20个组合是巨大的,不可用在可接受的时间计算。

    你能帮我指点方向或完整的解决方法来解决这个问题吗?

    谢谢:

    1 回复  |  直到 7 年前
        1
  •  2
  •   juvian    7 年前

    您可以使用dijkstra,其中每个州都有哪些人被留下。我们可以使用位掩码来表示仍然需要到达所需楼层的人员,第i个位置的1表示第i个人员已经到达所需楼层。

    过渡意味着与一组仍需到达所需楼层的人员进行一次旅行,从而改变状态,使旅行中每个人的第i个位置都有1。因为有20个客户机,所以有2^20个可能的状态。这是我在c++中提出的dijkstra解决方案,最多支持20个人。

    #include <iostream>
    #include <vector>
    #include <set>
    #include <map>
    
    using namespace std;
    
    struct Person {
        int weigth;
        int height;
    
        Person (int w, int h) {
            weigth = w;
            height = h;
        }
    };
    
    set<pair<int, int> > s;
    int maxWeigth = 200;
    vector<Person> persons;
    int distances[1 << 21];
    int currentCost;
    
    void visitNeighbors(vector<int>& remainingPersons, int state, int weigthSum, int maxHeight, int index) {
        if (weigthSum > maxWeigth) return;
    
        if (index != 0) {
            if (distances[state] == -1 || currentCost + maxHeight < distances[state]) {
                distances[state] = currentCost + maxHeight;
                s.insert(make_pair(distances[state], state));
            }
        }
    
        if (index == remainingPersons.size()) return;
    
        visitNeighbors(remainingPersons, state | (1 << remainingPersons[index]), weigthSum + persons[index].weigth, max(maxHeight, persons[index].height), index + 1);
        visitNeighbors(remainingPersons, state, weigthSum, maxHeight, index + 1);
    }
    
    int main () {
        persons.push_back(Person(90, 170));
        persons.push_back(Person(80, 160));
        persons.push_back(Person(100, 150));
    
        fill(distances, distances + (1 << 21), -1);
    
        int target = (1 << (persons.size())) - 1; // 111 means the 3 persons have already arrived at desired floor
    
        s.insert(make_pair(0, 0)); // initial state is with 0 cost and no person on the desired floor
        distances[0] = 0;
    
        while (!s.empty()) {
            pair<int, int> p = *s.begin();
            s.erase(s.begin());
    
            currentCost = p.first;
            int state = p.second;
    
            vector<int> remainingPersons;
    
    
            if (distances[state] != -1 && distances[state] < currentCost) continue;
            if (state == target) break;
    
    
            for (int i = 0; i < persons.size(); i++) {
                if ((state & (1 << i)) == 0) { // if we have a 0 at index i on state as a binary string, we still need to move that person
                    remainingPersons.push_back(i);
                }
            }
    
            visitNeighbors(remainingPersons, state, 0, 0, 0);       
        }
    
        cout << distances[target] << endl;
    
    }
    

    js实现:

    var maxWeigth = 200;
    
    var persons = [{
    	height: 170,
      weigth: 90
    }, 
    {
    	height: 160,
      weigth: 80
    },
    {
    	height: 150,
      weigth: 100
    }];
    
    var distances = new Array(1 << persons.length);
    distances.fill(-1);
    
    var currentCost;
    
    var target = (1 << persons.length) - 1;
    
    var queue = new PriorityQueue({ comparator: (a, b) => a.cost - b.cost});
    
    queue.queue({cost: 0, mask: 0});
    distances[0] = 0;
    
    while(queue.length) {
    	var state = queue.dequeue();
      
      if (distances[state.mask] != -1 && distances[state.mask] < state.cost) continue;
      if (state.mask == target) break;
      
      var remainingPersons = []
      currentCost = state.cost;
      
      for (var i = 0; i < persons.length; i++) {
        if ((state.mask & (1 << i)) == 0) { 
          remainingPersons.push(i);
        }
      }
    
      visitNeighbors(remainingPersons, state.mask, 0, 0, 0);
    }
    
    console.log(distances[target])
    
    
    function visitNeighbors(remainingPersons, mask, weigthSum, maxHeight, index) {
        if (weigthSum > maxWeigth) return;
    
        if (index != 0) {
            if (distances[mask] == -1 || currentCost + maxHeight < distances[mask]) {
                distances[mask] = currentCost + maxHeight;
                queue.queue({cost: distances[mask], mask: mask});
            }
        }
    
        if (index == remainingPersons.length) return;
    
        visitNeighbors(remainingPersons, mask | (1 << remainingPersons[index]), weigthSum + persons[index].weigth, Math.max(maxHeight, persons[index].height), index + 1);
        visitNeighbors(remainingPersons, mask, weigthSum, maxHeight, index + 1);
    }
    <script src="https://cdn.rawgit.com/adamhooper/js-priority-queue/master/priority-queue.min.js"></script>