有趣的算法任务-电梯

时间:2018-06-22 17:24:26

标签: algorithm

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

从A到B有一部电梯。一趟的价格如下:

1)如果只有一个人-身高(以分表示)-如果是180-> 180分

2)如果超过一个人->最大高度:[180,150,185]->总计185美分。

电梯的上限为N kg或大于700 kg,例如 700kg。

您有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 个答案:

答案 0 :(得分:2)

您可以使用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>