Hoj 1005 fast food

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.