Fast Food
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1597   Accepted: 553   Special Judge

Description

The fastfood chain McBurger owns several restaurants 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 restaurants with the needed ingredients. Naturally, these depots should 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 restaurants 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 restaurants. 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 as

n
∑ |di - (position of depot serving restaurant i)|
i=1

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.

Input

The 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 restaurants, ordered increasingly.

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

Output

For each chain, first output the number of the chain. Then output an optimal placement of the depots as follows: for each depot output a line containing its position and the range of restaurants it serves. If there is more than one optimal solution, 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 3
5
6
12
19
20
27
0 0

Sample Output

Chain 1
Depot 1 at restaurant 2 serves restaurants 1 to 3
Depot 2 at restaurant 4 serves restaurants 4 to 5
Depot 3 at restaurant 6 serves restaurant 6
Total distance sum = 8

Source

Southwestern European Regional Contest 1998
 
 

【题目大意】

一条公路上有n个旅馆,选出其中k个设置仓库,一个仓库可服务若干个旅馆,一个旅馆只需一个仓库服务。问在哪几个旅馆设置仓库,每个仓库服务哪些旅馆,可使得旅馆到仓库的总距离最小,并求出总距离(长理只要求求最后一步)。

【数据范围】

1 <= n <= 200, 1 <= k <= 30, k <= n

【解题思想】

1、此题属于明显动态规划题,关键点是找状态转移方程。

2、可以用sum[i][j]表示前i个旅馆,设置j个仓库得到的距离和最小值,那么sum[n][k]即为所求。

3、找sum[i][j]的子结构,假设前j-1个仓库服务第1个到第k个旅馆,则最后一个仓库服务第k+1个到第i个旅馆。

4、可以用one[i][j]表示一个仓库服务第i个到第j个旅馆,到这个仓库距离和的最小值。

5、则得到状态转移方程:sum[i][j]=min(sum[k][j-1]+one[k+1][i]) (j-1<=k<=i-1,min表示所有k取值得到的值中的最小值)。

6、问题转换为了求one[i][j],即在第i到第j家旅馆中设置一个仓库的总距离。

7、假设i到j共有奇数家旅馆,我们尝试将仓库放置在中间旅馆,即旅馆(i+j)/2,假设将仓库左移距离x,则右半边所有旅馆到仓库距离均加x,而只有部分左半边旅馆距离减少了x,剩下的减少均小于x,甚至不减少。因此可以得到,将仓库从中间位置左移到任何位置总距离都会增加,右移同理,因此仓库放到旅馆(i+j)/2最合适。

8、假设i到j共有偶数家旅馆,容易得到将仓库放到(i+j-1)/2和(i+j+1)/2得到的总距离相等(对称性),若将仓库放到(i+j-1)/2,并左移,则用7相似的想法可得知总距离增大,右移情况同理,由此得知仓库放到(i+j-1)/2这个位置即可满足总距离最小。

9、由7、8得到one[i][j]实际上时将仓库放到(i+j)/2取整位置可得到最小的总距离。

10、数据范围较小,我们可以计算出一切one[i][j]的组合。

 

11、由于poj还要求输出在哪几个旅馆设置仓库,每个仓库服务哪些旅馆,因此还需要存储动态规划路径。

12、可用at[i][j],from[i][j],to[i][j]分别表示sum[i][j]得到最小值时最后一个仓库的位置、服务的起始位置和服务的终止位置。

13、通过递归输出结果。

 

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int INF=100000000;
int r[300],sum[300][40],one[300][300];

int from[300][40],to[300][40],at[300][40];

int output(int i,int j)
{
if(j<=0||i<=0)return 1;
int num=output(from[i][j]-1,j-1);
printf("Depot %d at restaurant %d serves ",num,at[i][j]);
if(from[i][j]==to[i][j])printf("restaurant %d\n",from[i][j]);
else printf("restaurants %d to %d\n",from[i][j],to[i][j]);
return num+1;
}

int main()
{
int n,K,i,j,k,middle;
int iCase=0;
while(scanf("%d%d",&n,&K)!=EOF)
{
iCase++;
if(n==0&&K==0)break;
for(i=1;i<=n;i++)scanf("%d",&r[i]);
memset(one,0,sizeof(one));
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
middle=(i+j)/2;
for(k=i;k<middle;k++)one[i][j]+=r[middle]-r[k];
for(k=middle+1;k<=j;k++)one[i][j]+=r[k]-r[middle];
}
}
for(i=1;i<=n;i++)sum[i][0]=INF;
for(i=1;i<=n;i++)
{
for(j=1;j<=i&&j<=K;j++)
{
sum[i][j]=INF;
for(k=j-1;k<=i-1;k++)
{
int tmp=sum[k][j-1]+one[k+1][i];
if(tmp<sum[i][j])
{
sum[i][j]=tmp;
from[i][j]=k+1;
to[i][j]=i;
at[i][j]=(k+1+i)/2;
}
}
}
}
printf("Chain %d\n",iCase);
output(n,K);
printf("Total distance sum = %d\n\n",sum[n][K]);
}
return 0;
}

 


文章来源: http://www.cnblogs.com/kuangbin/archive/2011/11/12/2246407.html