HDU-1227dp

Fast Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3402    Accepted Submission(s): 1460


Problem 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 3 5 6 12 19 20 27 0 0
 

Sample Output
 
  
Chain 1 Total distance sum = 8

题意:输入n,k表示n个商店和k个仓库,后面的表示商店的位置。问在哪些位置中建立仓库使得所有商店到仓库的距离最短(仓库是建立在商店中的)

思路:(看了大佬的思路才知道怎么做)首先我们先记录仓库在所有商店的情况,设从 i 到 j 商店中的一家商店建立仓库,那么仓库建立的地方一定是 (i+j)/2  这样才能保证 i 到 j 的商店到仓库的距离最短 (中位数)

dis[ i ] [ j ] = dis[ i ] [ j ] + abs( num[ k ] - num[ ( i + j ) / 2] )       (num[ k ] 表示 k 商店的位置)

k表示 i ~ j 中的所有商店,( num[ k ] - num[ ( i + j ) / 2] ) 表示的是 k 商店到仓库的距离,可能有负数,加个abs

然后我们用dp方程来求最短距离

dp[ i ] [ j ]  i 表示 i 个仓库  , j 表示前 j 个商店,dp[ i ] [ j ]表示前 j 个商店中有 i 个仓库的最短距离

这样可以推出dp方程:

dp[ i ] [ j ] = min ( dp[ i ] [ j ] , dp[ i - 1 ] [ k ] + dis[ k + 1 ] [ j ] );

dp[ i - 1 ] [ k ] 表示前 k 商店有 i - 1 个仓库的最短距离 再加上 dis[ k + 1 ] [ j ] ,

dis[ k + 1 ] [ j ] 表示 k + 1 到 j 商店 建立一个仓库的最短距离 

所以这就是 前 j 商店建立 i 个仓库的最小值

坑点:结尾换行应该算

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=220;
const int INF=0x7f7f7f7f;
int dp[40][maxn],num[maxn],dis[maxn][maxn];
int main()
{
    int m,n,k,i,j,l;
    l=1;
    while (cin>>n>>k&&(n+k))
        {
            memset(num,0,sizeof(num));
            memset(dis,0,sizeof(dis));
            for (i=1;i<=n;i++)
                cin>>num[i];
            for (i=1;i<=n;i++)
                for (j=i+1;j<=n;j++)
                    for (m=i;m<=j;m++)
                        dis[i][j]=dis[i][j]+abs(num[m]-num[(i+j)/2]);
            for (i=1;i<=n;i++)                    //这里是初始化,也可以理解成,假如只有一个仓库,那么这就是最优的情况
                dp[1][i]=dis[1][i];
            for (i=2;i<=k;i++)
                for (j=i;j<=n;j++)
                    {
                        dp[i][j]=INF;
                        for (m=i-1;m<j;m++)
                            dp[i][j]=min(dp[i][j],dp[i-1][m]+dis[m+1][j]);
                    }
            printf("Chain %d\n",l++);
            printf("Total distance sum = %d\n",dp[k][n]);
            cout<<endl;                //注意结尾还要换行
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/81060997
今日推荐