Jury compromise (POJ 1015)

Source: Internet
Author: User

http://acm.fzu.edu.cn/problem.php?pid=1005

Description

The Fastfood chain Mcburger owns several restaurants along a highway. Recently, they has decided to build several depots along the highway, each one located at a restaurant and supplying Seve Ral of the restaurants with the needed ingredients. Naturally, these depots should be placed so, the average distance between a restaurant and its assigned depot is Minim Ized. You is to write a program that computes the optimal positions and assignments of the depots.

To do this more precise, the management of Mcburger have issued the following specification:you would be given the Positi ONS of n restaurants along the highway as n integers d1 < D2 < ... < DN (these is the distances measured from th E company's headquarter, which happens to being at the same highway). Furthermore, a number k (k <= N) would be given, and the number of depots to be built.

The k depots is built at the locations of K different restaurants. Each restaurant'll is assigned to the closest depot and from which it'll then receive its supplies. To minimize shipping costs, the total distance sum, defined as

Must be as small as possible.

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

Input

The input file contains several descriptions of fastfood chains. Each description starts with a line containing the integers n and K. N and K would satisfy 1 <= n <=, 1 <= K <=, K <= N.  Following this would n lines containing one integer each, giving the positions di of the restaurants, ordered increasingly.

The input file would end with a case starting with n = k = 0. This case is should not being processed.

Output

for 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 35 60 0

Sample Output

Chain 1 Total distance sum = 8 Main topic: give you n k, n means there are n points, let you choose K points, the sum of the n points to reach any point in the K-point minimum distance   n个旅馆和k个补给站的问题

假设有3个旅馆坐标分别是 1, 4, 5, 和2个补给站,那么路程代价就是1了,一个补给站放在坐标为1的旅馆那,令一个放在4位置处。
也可以一个补给站放在坐标为 1 的旅馆那,令一个放在 5 位置处。   //dp[i][k]表示前i个店添加k个供应点所达到的最小值 //状态转移方程为:dp[i][k] = min(dp[j][k-1], sum[j+1][i]), //其中k-1 <= j <= i-1, sum[i][j]表示从第i个饭店到第j个饭店添加一个供应点所达到的最小值,取i,j中间值即可
#include <cstdio>#include<cstring>#include<iostream>#include<cmath>#include<vector>#include<algorithm>#include<string>#include<map>using namespacestd;#defineN 220#defineMOD 1000000007#defineMet (A, b) memset (A, B, sizeof (a))#defineINF 0x3f3f3f3fintDp[n][n], sum[n][n], a[n];intMain () {intN, M, icase=1;  while(SCANF ("%d%d", &n, &m), n| |m) {intI, j, K;  for(i=1; i<=n; i++) scanf ("%d", &A[i]);  for(i=1; i<=n; i++) {Sum[i][i]=0;  for(j=i+1; j<=n; J + +) {Sum[i][j]= sum[i][j-1] + a[j]-a[(I+J)/2]; }        }         for(i=0; i<=n; i++)         for(j=0; j<=m; J + +) Dp[i][j]=INF; dp[0][0] =0;  for(i=1; i<=n; i++)        {             for(k=1; k<=m; k++)            {                 for(j=k-1; j<i; J + +) {Dp[i][k]= Min (Dp[i][k], dp[j][k-1]+sum[j+1][i]); }}} printf ("Chain%d\n", icase++); printf ("Total Distance sum =%d\n\n", Dp[n][m]); }    return 0;}

Jury compromise (POJ 1015)

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.