There are n hotels, and K are selected to create warehouses. The X coordinates of N, K, and N hotels are given. Each hotel Obtains resources from the nearest warehouse, minimizing the distance between all hotels and warehouses.
Idea: see the code
# Include <stdio. h> # include <math. h> // M [I, j] indicates the minimum distance and // W [I, j] of the first J Hotels for building I warehouses .. j] minimum distance between building a warehouse and int w [201] [201], M [31] [201], d [201]; int main () {int N, p, Case = 1; while (scanf ("% d", & N, & P), n) {int I, J, K, T; for (I = 1; I <= N; I ++) scanf ("% d", & D [I]); for (I = 1; I <= N; I ++) {W [I] [I] = 0; // Boundary Problem for (j = I + 1; j <= N; j ++) {// the odd number in the middle, even numbers are created in the middle. Both numbers can be W [I] [J] = W [I] [J-1] + d [J]-d [(I + J) /2] ;}}for (j = 1; j <= N; j ++) m [1] [J] = W [1] [J]; // Boundary Question for (I = 2; I <= P; I ++) {M [I] [I] = 0; For (j = I + 1; <= N; j ++) {M [I] [J] = 100000000; For (k = I-1; k <j; k ++) {// minimum value of cyclic solution T = m [I-1] [k] + W [k + 1] [J]; If (T <m [I] [J]) M [I] [J] = T ;}} printf ("chain % d \ ntotal distance sum = % d \ n", Case ++, M [p] [N]);} return 0 ;}