hdu 1227 fast food 动态规划

来源:互联网 发布:易语言仿ce源码 编辑:程序博客网 时间:2024/05/21 14:06
 Fast Food
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1227

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 



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 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
 
动态规划,用c[i][j]表示在i和j之间建一个仓库花费的最小距离,注意:ij之间建仓库要使距离最小应该建在a[(i+j)/2]上。
dp[i][j]表示前j个店建i个仓库的最小距离。状态转移方程:dp[i][j]=min{dp[i-1][k]+c[k+1][j]}(k=i-1,i,...,j-1。)
代码:
#include<stdlib.h>#include<limits.h>#include<string.h>#include<stdio.h>int main(){    int dp[202][202],c[202][202];    int n,m,a[202],tt=1;    while(scanf("%d%d",&n,&m),n,m)    {        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        memset(dp,0,sizeof(dp));        memset(c,0,sizeof(c));        for(int i=1;i<=n;i++)        {            for(int j=i;j<=n;j++)            {                for(int k=i;k<=j;k++)                {                    c[i][j]+=abs(a[k]-a[(i+j)/2]);                }                //printf("c%d %d %d\n",i,j,c[i][j]);            }        }        for(int i=1;i<=n;i++)        {            dp[1][i]=c[1][i];        }        for(int i=2;i<=m;i++)        {            for(int j=i+1;j<=n;j++)            {                int m=INT_MAX;                for(int k=i-1;k<j;k++)                {                    if(m>dp[i-1][k]+c[k+1][j])                        m=dp[i-1][k]+c[k+1][j];                }                dp[i][j]=m;                //printf("dp%d %d %d\n",i,j,dp[i][j]);            }        }        printf("Chain %d\n",tt++);        printf("Total distance sum = %d\n",dp[m][n]);        printf("\n");    }}


0 0
原创粉丝点击