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.