[dp] poj1160 Post office

31 篇文章 0 订阅
25 篇文章 0 订阅

Description

There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

Input

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

Output

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

Sample Input

10 5
1 2 3 6 7 9 11 22 44 50

Sample Output

9

题目分析

    状态不难考虑,令f[i][j]为前i个村庄设立j个邮局的最小距离和。
    和经典的dp题目相同,考虑最后一个——第j个邮局,它可以让从第k+1个村庄(含)到第i个村庄(含)都以它为最近邮局。由于前j-1个邮局至少要在j-1个村庄里,故j-1 < k+1 <= i,即j-1 < = k < i。
    而在k+1~i的村庄中,显然将邮局设在尽可能靠近中间的位置总和最小。设d[i][j]为从i到j村庄设立一个邮局得到的最小距离总和,则
    d[i][j]=d[i][j-1]+(a[j]-a[(l+r)/2])
    为什么这样是正确的呢?可以分两种情况来考虑:
    1. 若i到j-1是奇数个村庄,则增加一个村庄后,其“中位村庄”(即(i+j)/2,离正中间距离最近的村庄)仍可不变。举例来说,i到j-1有5个村庄,以第三个村庄为中位,递推到i到j的村庄时,有6个村庄,中位村庄是第三个或第四个(这两个村庄无论选哪一个为邮局结果都一样,可以验证)
    2. 偶数同理
    再结合前面关于f[i][j]计算的方法,可得到状态转移方程:
    f[i][j]=min(f[i][j],f[k][j-1]+d[k+1][i])(其中j-1 < = k < i)
    最后注意初始化f[i][i]=0

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int f[301][31];
int a[301],d[301][301];
int min(int,int);
int main()
{
    int m,n;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
        cin>>a[i];
    memset(d,0,sizeof d);
    for(int i=1;i<m;i++)
        for(int j=i+1;j<=m;j++)
        {
            int mid=a[(i+j)/2];
            for(int k=i;k<=j;k++)
                d[i][j]+=abs(a[k]-mid);
        }
    memset(f,0x3f,sizeof f);
    for(int i=0;i<=n;i++)f[i][i]=0;
    for(int j=1;j<=n;j++)
        for(int i=j+1;i<=m;i++)
            for(int k=j-1;k<i;k++)
                f[i][j]=min(f[i][j],f[k][j-1]+d[k+1][i]);
    cout<<f[m][n]<<endl;
    return 0;
}
int min(int a,int b)
{return (a<b)?a:b;}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值