uva 662 - Fast Food

来源:互联网 发布:加工中心怎么编程铣面 编辑:程序博客网 时间:2024/05/16 00:43

Fast Food 

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 restaurent 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 $d_1 < d_2 < \dots < d_n$ (these are the distances measured from the company's headquarter, which happens to be at the same highway). Furthermore, a number $k (k \leŸ 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


\begin{displaymath}\sum_{i=1}^n \mid d_i - (\mbox{position of depot serving restaurant }i) \mid\end{displaymath}

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 kn and k will satisfy $1 \leŸ n\leŸ 200$$1 \leŸ k Ÿ\le 30$$k \le 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 356121920270 0

Sample Output 

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

这道题难度其实不大,仔细分析就能想到dp[i][j]表示前i个餐厅设置j个仓库最小代价。

假设第j个仓库管辖范围[k,i],这里说明一点,每个仓库的管辖范围肯定是一段连续餐厅区间,否则不可能最优,这个留个你们去证明。

那么dp[i][j]=max{dp[k-1][j-1]+dis[k][i]},j<=k<=i,这里枚举i,j,k是必须的,复杂度已达O(kn^2),因此dis数组必须在O(1)的时间内得到。

dis[i][j]说白了是在第i个餐馆到第j个餐馆之间选个位置建个仓库使代价最小,这个显然是中位数。不明白的可以去翻翻中位数的性质:到其他数的距离之和最小。

求dis[i][j],枚举i,j复杂度已达O(n^2),因此每个dis[i][j]都必须在O(1)时间里得到,可以再来次dp,dis[i][j]=dis[i][j-1]+a[j]-a[i+j>>1];

或者可以观察一下规律:把所有距离和累加,会发现其实是若干个a[i+j>>1]和两端段区间和相加减。两种方法都可以。这里用第一种了!

代码:

#include<cstdio>#include<iostream>#define Maxn 210using namespace std;int a[Maxn],dis[Maxn][Maxn],dp[Maxn][40],path[Maxn][40];const int inf=1<<30;void print(int n,int m){    if(m==0) return;    int s=path[n][m]/201,e=path[n][m]%201;    print(s-1,m-1);    printf("Depot %d at restaurant %d serves restaurant",m,s+e>>1);    if(s==e) printf(" %d\n",s);    else printf("s %d to %d\n",s,e);}int main(){    int n,m,cas=1;    while(~scanf("%d%d",&n,&m),n){        for(int i=1;i<=n;i++){            scanf("%d",a+i);            dis[i][i]=0;        }        for(int l=1;l<n;l++)            for(int i=1,j=1+l;j<=n;i++,j++)                dis[i][j]=dis[i][j-1]+a[j]-a[i+j>>1];        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                dp[i][j]=inf;        for(int i=1;i<=n;i++){            dp[i][1]=dis[1][i];            path[i][1]=201+i;        }        for(int i=2;i<=n;i++)            for(int j=2;j<=min(i,m);j++)                for(int k=j;k<=i;k++){                    if(dp[k-1][j-1]+dis[k][i]<dp[i][j]){                        dp[i][j]=dp[k-1][j-1]+dis[k][i];                        path[i][j]=k*201+i;                    }                }        printf("Chain %d\n",cas++);        print(n,m);        printf("Total distance sum = %d\n\n",dp[n][m]);    }return 0;}


0 0
原创粉丝点击