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