I - Post Office POJ - 1160 (动态规划)

I - Post Office

 POJ - 1160 

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

 dp[x][num]表示从开始到x位置建立num个邮局的总耗费,动态规划讲究从已知子问题结果中寻找答案,那么容易想到从建立一个邮局到p个问题的解题过程,假设当前建立i个邮局,而前面到位置j(j>=i-1&&j<=n)建立i-1个邮局的最优解已经得到,那么求到位置j建立i个邮局的问题就转换为在j位置前找一个位置k(k>=i&&k<=j),让[k,j]作为建立第j个邮局的作用域(即[k,j]到第j个邮局最近),使在位置j前建立i个邮局有最优解。

于是有状态转换方程:

dp[j][i]=min{dp[k-1][i-1]+sum[k][j]}  //在位置j前建立i个邮局

求sum数组的优化问题,参考了网上的解析:可以假定有6个村庄,村庄的坐标已知分别为p1,p2,p3,p4,p5,p6;那么,如果要求sum[1][4]的话邮局需要建立在2或者3处,放在2处的消耗为p4-p2+p3-p2+p2-p1=p4-p2+p3-p1
放在3处的结果为p4-p3+p3-p2+p3-p1=p4+p3-p2-p1,可见,将邮局建在2处或3处是一样的。现在接着求sum[1][5],现在处于中点的村庄是3,那么1-4到3的距离和刚才已经求出了,即为sum[1][4],所以只需再加上5到3的距离即可。同样,求sum[1][6]的时候也可以用sum[1][5]加上6到中点的距离。所以有递推关系:sum[i][j] = sum[i][j-1] + p[j] -p[(i+j)/2]

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=330;
const double esp = 1e-9;
const double PI=3.1415926;
int dp[N][N],sum[N][N],a[N];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
      for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
      memset(sum,0,sizeof(sum));
      for(int i=1;i<n;i++){
            for(int j=i+1;j<=n;j++){
                  sum[i][j]=sum[i][j-1]+a[j]-a[(i+j)/2];
            }
      }
      memset(dp,inf,sizeof(dp));
      for(int i=1;i<=n;i++)
            dp[i][1]=sum[1][i];
      for(int i=2;i<=m;i++){
            for(int j=i;j<=n;j++){
                  for(int k=i;k<=j;k++)
                        dp[j][i]=min(dp[j][i],dp[k-1][i-1]+sum[k][j]);
            }
      }
      printf("%d\n",dp[n][m]);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值