在排序后的数组最低成本路径

编程入门 行业动态 更新时间:2024-10-10 05:25:21
本文介绍了在排序后的数组最低成本路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给出一个排序的数组 A 如 {4,9,10,11,19} 。成本从 I-&GT运动;Ĵ是 ABS(A [J] -A [I])。从一个给定的元件例如启动 10 。找出没有访问相同的元素两倍的最低成本路径。因此,在这个例子中的解决办法是 10→9> 4- GT; 11-> 19 即 1 + 5 + 7 + 8 = 21 。

Given a sorted array A e.g. {4,9,10,11,19}. The cost for moving from i->j is abs(A[j]-A[i]). Start from a given element e.g. 10. Find out the least cost path without visiting same element twice. So in this example solution would be 10->9->4->11->19 i.e. 1 + 5 + 7 + 8 = 21.

我试图解决这个使用最邻近的方法。

I tried to solve this using nearest neighbor approach.

i = start; While (array is not empty) ldiff = A[i] - A[i-1] rdiff = A[i+1] - A[i] (ldiff < rdiff) ? sum += ldiff : sum += rdiff remove A[i]

该解决方案不会在任何情况下工作。我已经意识到这是TSP问题。有什么能解决这个问题最好的办法?我将使用TSP的启发式像赫里斯托菲或其他算法?

This solution does not work in every case. I have realised that this is TSP problem. What could be the best approach to solve this problem? Shall I use TSP heuristics like Christofides or some other algorithm?

推荐答案

对于你的情况最少的费用是21,看

for your case least cost is 21, see

10->9->4->11->19 ==> 1 + 5 + 7 + 8 = 21.

我想,对于通常情况下,如果从第i个起始位置,最少成本路径,最短

I think, for common case, if you start from i-th position, least cost is path, minimum of

A[i]->A[i-1]-> ...->A[1] -> A[i+1] -> A[i+2] -> ...->A[n] and A[i]->A[i+1]-> ... ->A[n] ->A[i-1] ->A[i-2] -> ...->A[1]

更多推荐

在排序后的数组最低成本路径

本文发布于:2023-11-30 13:43:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   路径   最低   成本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!