POJ 1160 Post Office(dp)

Post Office

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18758 Accepted: 10113

戳这里题目链接


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


题目大意:直线上有v个村庄,选择其中p个村庄建立邮局,使得每个村庄到距离其最近的邮局距离之和最短。
题目思路:在只有一个邮局的时候,很容易想到建在最中间是距离和最小的时候,当再增加一个邮局之后,我们可以考虑为最后这一个邮局管的范围是最后几个村庄,而之前的已经存在的邮局则负责前几个村庄的最短距离和。这样,我们可以首先进行预处理,求出每两个村庄i、j之间所有村庄的最短距离和,存为cost[i][j]数组。
可设dp[v][p]数组,d[i][j]代表i个村庄建立p个邮局时的最优解,有如下状态转移方程:
dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[j][p])
初始化时我们可将i==j的情况下的dp[i][j]赋值为0,其他可赋值为INF。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;

int dp[305][35],cost[305][305],a[305];
int calcost(int i,int j)
{
    int ans=0;
    while(i<j)
    {
        ans+=a[j]-a[i];
        i++;j--;
    }
    return ans;
}

int main()
{
    int v,p,i,j,k;
    while(scanf("%d%d",&v,&p)!=EOF)
    {
        for(i=1;i<=v;i++)
            scanf("%d",&a[i]);
        for(i=1;i<=v;i++)
            for(j=i;j<=v;j++)
            cost[i][j]=calcost(i,j);
        for(i=0;i<=v;i++)
            for(j=0;j<=p;j++)
            if(i==j) dp[i][j]=0;
            else dp[i][j]=INF;

        for(i=1;i<=v;i++)
            for(j=1;j<=p;j++)
            for(k=j-1;k<i;k++)
        {
            dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[k+1][i]);
        }
        cout<<dp[v][p]<<endl;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值