最小化机械成本的算法

dev*_*sda 6 algorithm dynamic-programming greedy data-structures

有一条直路.并且在某种程度上存在一些工作.每项工作都有一定的重要性.

现在,从数学上讲,我们得到了一系列的位置location[].并且给出了重要数组,importance[].

如果你从一个点移动到另一个点,机械师需要走路,并在那个位置做工作.

而且,机械师需要完成所有工作.

我们需要最小化(重要性*距离)的总和.

更好理解的例子: -

position     :-  1    6    12   13    14      24
importance   :-  1    2    10   3     5       1
Run Code Online (Sandbox Code Playgroud)

如果我们从6开始,并按照以下方式移动

  6  -->   12 --->  13 ---> 14 --> 1 --> 24
Run Code Online (Sandbox Code Playgroud)

=(10*6)+(3*7)+(5*8)+(1*21)+(1*44)= 186

如果我们按照以下方式行事,

12 --> 13  --> 14 -->  6 --> 1 --> 24
Run Code Online (Sandbox Code Playgroud)

=(3*1)+(5*2)+(2*10)+(1*15)+(1*38)= 86

So the minimum answer is 86
Run Code Online (Sandbox Code Playgroud)

我的方法

首先,我们需要计算从哪里开始.为此寻找重心.

对于位置= 1,(5*2)+(11*10)+(12*3)+(13*5)+(23*1)= 244

对于位置= 6,(5*1)+(6*10)+(7*3)+(8*5)+(18*1)= 144.

类似地计算所有位置,并选择最小值,即Center of gravity.

但在选择这一点之后,如何转移到其他位置对我来说是遥不可及的.

但我无法完成算法.请帮忙.