Supply depots
A grocery chain owns several stores along a road. They wish to build several depots along the road, each one located at a store and supplying several of the stores with materials. Naturally, these depots should be placed so that the average distance between a store and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.
More formally, you are given the positions of n stores along the
highway as n integers
(these are the distances measured from the company's headquarter, which
happens to be on the same road). Furthermore, a number
will be given, the number of depots to be built. The k depots
will be built at the locations of k different stores. Each store will
be assigned to the closest depot, from which it will then receive its supplies.
To minimize transportation costs, the distance sum, defined as
The input file will end with a case starting with n = k = 0. This case should not be processed.
.
6 3
5
6
12
19
20
27
0 0
Chain 1 : Total distance sum = 8.