【DP|数学+预处理】HDU-1227 Fast Food

69 篇文章 0 订阅

Fast Food

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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个点运送物资,使得运送物资的距离之和最小。

思路:数学知识——中位数的性质:

中位数距离其它所有数的距离之和最小。

证明:

1. 首先证明为什么是中位数而不是左右端点的平均值
数学归纳法:k = 1成立;对于k = 3
例:1 9 12
1~12的平均值6.5到两端的距离之和是5.5+5.5=11
9到两端的距离之和是8+3=11
6.5到9的距离是2.5然而9到9的距离之和是0
k为偶数同理

2. 再证明为什么是中位数而不是其它某一个数

中位数左边有x个数,右边就一定是x个数

......m-1 m ......

假设中位数m和它左边的数m-1之间距离为a

那么选择m-1的话,左边就有x个点减少了距离a,即-x*a

右边就有x+1个点增加了距离a,即+(x+1)*a

显然不划算。对于m+1同理

既如此,我们可以预处理出对所有的(i~j)区间的最小距离和

设dp[i][j]表示前j个城市设置i个基地的最小距离和

那么它一定是由dp[i-1][k](0<=k<j)转移过来的

状态转移方程:dp[i][j] = min{dp[i-1][k] + cost[k+1][j]} (0<=k<j)

(k后面的城市属于新添加的区间,这个区间的花费需要预处理)

P.S. 数据水,网上的错误代码也能过啊

代码如下:

/**
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define For(i, x, y) for(int i=x; i<y; i++)
#define For_(i, x, y) for(int i=x; i>=y; i--)
#define Mem(f, x) memset(f, x, sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Pri(x) printf("%d\n", x)
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 222;
int n, m, dp[33][N], loc[N], pre[N], cost[N][N];

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    freopen("999.out", "w", stdout);
#endif
    int kase = 1;
    while(scanf("%d%d", &n, &m), n||m) {
        For(i, 0, n) {
            Sca(loc[i]);
        }
        For(i, 0, n) {
            For(j, 0, n) {
                cost[i][j] = 0;
                int mid = (i+j)>>1;
                For(k, i, j+1) {
                    cost[i][j] += abs(loc[mid] - loc[k]);
                }
            }
        }
        For(j, 0, n) {
            dp[0][j] = cost[0][j];
        }
        //dp[i][j] = min{dp[i-1][k] + cost[k+1][j]}
        For(i, 1, m) {
            For(j, 0, n) {
                int ret = INF;
                For(k, 0, j) {
                    ret = min(ret, dp[i-1][k] + cost[k+1][j]);
                }
                dp[i][j] = ret;
            }
        }
        printf("Chain %d\n", kase++);
        printf("Total distance sum = %d\n\n", dp[m-1][n-1]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值