[ACM] HDU 1227 fast food (Classic DP)

Source: Internet
Author: User
Fast food

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2173 accepted submission (s): 930


Problem descriptionthe fastfood chain mcburger owns several extends ants along a highway. recently, they have decided to build several depots along the highway, each one located at a restaurant and supplying several of the specified ants with the needed ingredients. naturally, these depots shoshould be placed so that the average distance between a restaurant and its assigned depot is minimized. you are to write a program that computes the optimal positions and assignments of the depots.

To make this more precise, the management of mcburger has issued the following specification: you will be given the positions of N parameter ants along the highway as N integers D1 <D2 <... <DN (these are the distances measured from the company's headquarter, which happens to be at the same highway ). furthermore, a number K (k <= N) will be given, the number of depots to be built.

The K depots will be built at the locations of k different distributed ants. each restaurant will be assigned to the closest depot, from which it will then receive its supplies. to minimize shipping costs, the total distance sum, defined

Must be as small as possible.

Write a program that computes the positions of the K depots, such that the total distance sum is minimized.
 


 

Inputthe input file contains several descriptions of fastfood chains. each description starts with a line containing the two integers N and K. N and K will satisfy 1 <=n <= 200, 1 <= k <= 30, k <= n. following this will n lines containing one integer each, giving the positions di of the specified ants, ordered increasingly.

The input file will end with a case starting with N = k = 0. This case shocould not be processed.
 


 

Outputfor each chain, first output the number of the chain. Then output a line containing the total distance sum.

Output a blank line after each test case.
 


 

Sample Input
6 356121920270 0
 


 

Sample output
Chain 1Total distance sum = 8
 


 

Sourcesouthwestern Europe 1998

 

Solution:

There are n stores on one road, each of which has an X coordinate position and K warehouses. K warehouses must be placed on K of the N locations, each store receives supplies from the nearest warehouse. How can we store these K warehouses so that the distance from each store to the corresponding warehouse can be minimized and the minimum output value can be reached.

To solve this question, we must be aware of two points:

1. if you want to place a warehouse between location I and location J, you need to place it in (I + J)/Location 2, (I + J) /2 is an integer to ensure that the sum of the distances from I to J stores to warehouses is the shortest.

2. if K warehouses are placed in N locations according to the question, and the distance and shortest are obtained, the minimum value must be obtained. Therefore, each store receives supplies from the nearest warehouse, because if it is not closest, distance and certainly are not the shortest

Use DP [I] [J] to represent the first J stores with I warehouses

The state transition equation is:

DP [I] [J] = min (DP [I] [J], DP [I-1] [k] + cost [k + 1] [J]) i-1 <= k <= J-1

DP [I] [J] is coming from a previous state, and the first K stores have I-1 warehouses, K is uncertain, but can determine its scope, the smallest is the I-1 (put a warehouse on a store location), the largest is the J-1 (put the I warehouse on the J location ), cost [I] [J] is the minimum distance between I and j to place a warehouse, that is, the 1st point mentioned above.

 

Code:

# Include <iostream> # include <algorithm> # include <stdio. h> # include <cmath> # include <string. h> using namespace STD; int N, K; int DP [32] [1, 210]; // DP [I] [J] The first J stores have I warehouse int dis [210]; const int INF = 0x3f3f3f3f; int cost (int I, Int J) {int ans = 0; int mid = dis [(I + J)/2]; for (int K = I; k <= J; k ++) ans + = ABS (DIS [k]-mid); Return ans;} int main () {int c = 1; while (scanf ("% d ", & N, & K )! = EOF) {If (! N |! K) break; For (INT I = 1; I <= N; I ++) scanf ("% d", & dis [I]); memset (DP, INF, sizeof (DP); For (INT I = 1; I <= N; I ++) DP [1] [I] = Cost (1, I ); for (INT I = 2; I <= K; I ++) // the I-th warehouse for (Int J = 1; j <= N; j ++) // The first J stores {for (int K = I-1; k <= J-1; k ++) DP [I] [J] = min (DP [I] [J], DP [I-1] [k] + cost (k + 1, J ));} printf ("chain % d \ n", C ++); printf ("total distance sum = % d \ n", DP [k] [N]); printf ("\ n");} return 0 ;}


 

 

[ACM] HDU 1227 fast food (Classic DP)

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.