6

There is one straight road. And on some distance some job is present. And every job has some importance.

Now, mathematically, an array of locations is given to us, location[]. and there importance array is given, importance[].

If you move from one point to other, mechanic needs to walk, and do job at that location.

And, mechanic needs to do all jobs.

We need to minimize summation of (importance*distance).

Example for better understanding :-

position     :-  1    6    12   13    14      24
importance   :-  1    2    10   3     5       1

If we start from 6, and move in following way

  6  -->   12 --->  13 ---> 14 --> 1 --> 24

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

If we move in following way,

12 --> 13  --> 14 -->  6 --> 1 --> 24

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

So the minimum answer is 86

My approach

First we need to calculate from where we need to start. For that find center of gravity.

For position = 1, (5*2) + (11*10) + (12*3) + (13*5) + (23*1) = 244

For position = 6, (5*1) + (6*10) + (7*3) + (8*5) + (18*1) = 144.

Similarly calculate for all positions, and choose minimum value, i.e., Center of gravity.

But after choosing that point, how to move to other positions, is out of reach for me.

But I am not able to complete the algorithm. Please help in that.

devsda
  • 4,112
  • 9
  • 50
  • 87
  • Can you tell me, what is wrong with my algo ? – devsda Feb 24 '14 at 18:53
  • 1
    You only showed us half an algorithm, so we can't really tell you what's wrong with it. – Bernhard Barker Feb 24 '14 at 19:15
  • 1
    You sure the problem formulation is correct? I would have expected that the cost would be the sum of `importance * waiting_time`. Were `waiting_time` is the sum of all distances travelled before visting that position. In your formulation it is only the distance traveled since the last postion. – Stefan Feb 24 '14 at 19:17
  • @Stefen Yes you are right. See the example, it shows the same that you said. – devsda Feb 24 '14 at 19:21
  • @devnull read @Dukeling 's link, specifically Rob Teller's answer describing going from `v1 -> v2 -> v3` vs `v1 -> v3 -> v2`. – C.B. Feb 24 '14 at 19:21
  • @Dukeling I was totally chocked in this problem. The thing that I know is to find the point from where we can start. But i am not sure whether above method will work or not. – devsda Feb 24 '14 at 19:22
  • @devnull: I don't think greedy works here, so I guess your approach will not give you an optimal solution – Niklas B. Feb 24 '14 at 19:35
  • if your dataset is small enough then you can still try all combinations and use the best (N< 10..20) also as I understand importance is a simplifying weight (probably based on job frequency and material movement costs) so the real optimum can be different then the computed one (if the weights are not calculated very very carefully). – Spektre Feb 25 '14 at 08:58
  • anyone have a proof why we should start from centre of gravity? – cegprakash Feb 27 '14 at 06:37

0 Answers0