# Minimize the average distance from each cities to the closest hospitals

There are n cities [1, 2, 3, .... n] and k available hospitals. k < n. We need to place hospitals into the cities. How to place these hospitals to minimize the average distance from each cities to the closest hospitals.?

Right now my thought is using dynamic programming with a 2D array of size n * k. Then I know optimal solution will be f(n, k) = , which pump𝑘 is the position at the median of cities 𝑖+1,...,𝑛. I believe this is the right approach but I do not know how to store the location of each hospitals. Please help

New contributor
noname is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Just to check if I am understanding your problem correctly, Is your problem equivalent to minimizing the sum of the distances of each city to the closest hospital? – Inuyasha Yagami Jun 9 at 3:19
• @InuyashaYagami Yes!!! – noname Jun 9 at 3:41
• First, note that the problem is called the $k$-median problem which NP-hard. So, you can not expect any algorithm with a running time polynomial in $n$ and $k$ – Inuyasha Yagami Jun 9 at 3:43
• @InuyashaYagami Hi, I believe my run time will be n^2k. the n * k is just the 2d array for me to build up the final solution – noname Jun 9 at 3:46
• Secondly, what do you mean by the median of the cities? Is it $1$-median. In that case, $pump_{k}$ might not be located at some city. – Inuyasha Yagami Jun 9 at 3:46