Fast FoodTime
limit:3000MS
Memory Limit:0KB
64bit IO Format:%lld &%llu SubmitStatusPracticeUVA 662Appoint Description:System Crawler (2015-08-27)
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 restaurent 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 nrestaurants along the highway as n integers (these is the distances measured from the company ' S headquarter, which happens to being at the same highway). Furthermore, a number 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.
InputThe input file contains several descriptions of fastfood chains. Each description starts and a line containing the integers
Nand
k.
Nand
kwould satisfy,,. Following this would
NLines containing one integer each, giving the positions
D
IOf the restaurants, ordered increasingly.
The input file would end with a case starting with n = k = 0. This case is should not being processed.
OutputFor each chain, first output the number of the chain. Then output an optimal placement of the depots as Follows:for, depot output a line containing its position and the RA Nge of Restaurants it serves. If there is more than one optimal solution, and output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text.
Output a blank line after each test case.
Sample Input
6 356121920270 0
Sample Output
Chain 1Depot 1 at Restaurant 2 serves restaurants 1 to 3Depot 2 @ Restaurant 4 serves restaurants 4 to 5Depot 3 at Restau Rant 6 serves restaurant 6Total distance sum = 8
Miguel Revilla
2000-05-22
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 Const intinf=0x3f3f3f3f;6 7 inta[205],face[ *][205],dp[ *][205],cost[205][205];8 9 intPutintKintN)Ten { One if(k) A { - intup=face[k][n]+1, mid= (up+n)/2; -Put (K-1, Face[k][n]); the if(up==N) -printf"Depot%d at restaurant%d serves restaurant%d\n", k,mid,n); - Else -printf"Depot%d at restaurant%d serves restaurants%d to%d\n", k,mid,up,n); + } - return 0; + } A at intMain () - { - intn,k,mid,cas=1; - inti,j,k; - while(SCANF ("%d%d", &n,&k)! =EOF) - { inmemset (Cost,0,sizeof(cost)); - if(n==0&& k==0) to Break; + for(i=1; i<=n;i++) - { thescanf"%d",&a[i]); * } $ for(i=1; i<=n;i++)Panax Notoginseng { - for(j=i;j<=n;j++) the { +Mid= (I+J)/2; A for(k=i;k<=j;k++) the { +Cost[i][j]=cost[i][j]+abs (a[k]-A[mid]); - } $ } $ } - for(i=1; i<=n;i++) - { thedp[1][i]=cost[1][i]; - }Wuyi the for(i=2; i<=k;i++) - { Wu for(j=i;j<=n;j++) - { Aboutdp[i][j]=inf; $ for(k=i-1; k<j;k++) - { - if(Dp[i][j]> (dp[i-1][k]+cost[k+1][j])) -dp[i][j]=dp[i-1][k]+cost[k+1][j],face[i][j]=K; A } + } the } - $printf"Chain%d\n", cas++); the put (k,n); theprintf"Total Distance sum =%d\n\n", Dp[k][n]); the } the return 0; -}
View Code
Problem Wuva 66,223 Fast Food