Algorithm 最小化N人访问M个城市子集的旅行成本
我有一个在面试中被问到的问题 给出了Algorithm 最小化N人访问M个城市子集的旅行成本,algorithm,Algorithm,我有一个在面试中被问到的问题 给出了N旅行者、M城市的姓名以及城市间的人均旅行费用Mi和Mj,如Cij。每个旅行者Ni必须访问M地方的给定子集Si。和一个数字X 所有的旅行者都从一个共同的城市出发 他们可以访问不在他们要访问的城市列表中的城市 旅行者可以在一个城市停留任意时间 他们被要求返回原籍城市 所有城市都是相连的 一个人可以多次访问同一个城市 X
N
旅行者、M
城市的姓名以及城市间的人均旅行费用M
i和M
j,如C
ij。每个旅行者N
i必须访问M
地方的给定子集S
i。和一个数字X
- 所有的旅行者都从一个共同的城市出发
- 他们可以访问不在他们要访问的城市列表中的城市
- 旅行者可以在一个城市停留任意时间
- 他们被要求返回原籍城市
- 所有城市都是相连的
- 一个人可以多次访问同一个城市
X
T
有多少旅行者一起旅行,那么他们将节省(T-1)*X
两个城市之间旅行的总成本。我们需要确定每位旅行者的访问顺序,以便将所有旅行者的总体旅行成本降至最低
样本测试用例:
N = 4 M = 4
travellers[N] = { "person_one", "person_two", "person_three", "person_four" }
cities[M] = { "A", "B", "C", "D" }
costs = {
A <--> B = 100
A <--> C = 120
A <--> D = 50
B <--> C = 20
B <--> D = 20
C <--> D = 20
}
target_cities = {
person_one = { "A", "B" }
person_two = { "A", "B", "C", "D" }
person_three = { "A", "C", "D" }
person_four = { "A", "D" }
}
X = 10
Assuming they all start from city A.
person_one : A -> D ------> B -> D -> A // the extra spacing is for convenience in
person_two : A -> D -> C -> B -> D -> A // understanding, original output can have
person_three : A -> D -> C ------> D -> A // uniform spaces
person_four : A -> D ----------------> A
- they save 3*10 by travelling to D together
- person two and three save 1*10 from D -> C.
- person one and two travel back to D from B to save 3*10
travelling from D -> A.
输入:
N = 4 M = 4
travellers[N] = { "person_one", "person_two", "person_three", "person_four" }
cities[M] = { "A", "B", "C", "D" }
costs = {
A <--> B = 100
A <--> C = 120
A <--> D = 50
B <--> C = 20
B <--> D = 20
C <--> D = 20
}
target_cities = {
person_one = { "A", "B" }
person_two = { "A", "B", "C", "D" }
person_three = { "A", "C", "D" }
person_four = { "A", "D" }
}
X = 10
Assuming they all start from city A.
person_one : A -> D ------> B -> D -> A // the extra spacing is for convenience in
person_two : A -> D -> C -> B -> D -> A // understanding, original output can have
person_three : A -> D -> C ------> D -> A // uniform spaces
person_four : A -> D ----------------> A
- they save 3*10 by travelling to D together
- person two and three save 1*10 from D -> C.
- person one and two travel back to D from B to save 3*10
travelling from D -> A.
解释:
N = 4 M = 4
travellers[N] = { "person_one", "person_two", "person_three", "person_four" }
cities[M] = { "A", "B", "C", "D" }
costs = {
A <--> B = 100
A <--> C = 120
A <--> D = 50
B <--> C = 20
B <--> D = 20
C <--> D = 20
}
target_cities = {
person_one = { "A", "B" }
person_two = { "A", "B", "C", "D" }
person_three = { "A", "C", "D" }
person_four = { "A", "D" }
}
X = 10
Assuming they all start from city A.
person_one : A -> D ------> B -> D -> A // the extra spacing is for convenience in
person_two : A -> D -> C -> B -> D -> A // understanding, original output can have
person_three : A -> D -> C ------> D -> A // uniform spaces
person_four : A -> D ----------------> A
- they save 3*10 by travelling to D together
- person two and three save 1*10 from D -> C.
- person one and two travel back to D from B to save 3*10
travelling from D -> A.
到目前为止,我还没有找到解决这个问题的办法。我想知道我可以采取什么方法 这不是旅行推销员服用类固醇的问题吗?我同意n.m.的说法,这只是服用类固醇的TSP。这使得它在面试中无法回答。因此,在我看来,这个问题的目的是看看你对一项不可能完成的任务有何反应。你认为这是不可能的吗?你能解释一下各种方法,以及它们的优缺点吗?在手工做了这个例子之后,我认为解决方案是为每个人分别解决TSP问题。然后通过在较短的行程中增加延迟(调整行程日期)来最大化折扣。使用此技术,示例的答案是
one=“AD.BDA”
two=“ADCBDA”
three=“ADC.DA”
four=“AD…A”
,其中A
表示用于对齐旅行日期的延迟。此解决方案的总成本为460,而问题解决方案的总成本为620。这并不完美(因为某些行程可能会在不改变TSP成本的情况下重新安排),但这是一个很好的近似值。@user3386109这实际上包含在X