Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17168   Accepted: 9270

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个建立邮局,要求的是建成了P个邮局之后,所有村庄到邮局的距离最短。求最短距离。

冲着邮局的经典DP做的。暴力的话肯定TLE,所以只能往DP上想。

然后就从小了开始想,如果是从a点到b点(中间b-a+1个村庄)建立1个邮局的话,那肯定是要建在中间的村庄上没跑了。1和3的话,要建在2。1和4的话建在2或者3都行因为距离是一样的。

然后这个规律自己做得时候也发现了:

用sum[i][j]表示村庄i到村庄j建立一个邮局的距离。那么推导有sum[i][j]=sum[i][j-1]+value[j]-value[(i+j)/2]

你看,sum[1][4](建在2或者3)=(value[4]-value[3])+(value[3]-value[2])+(value[3]-value[1])

=value[4]+value[3]-value[2]-value[1]

而sum[1][5](建在3)=(value[5]-value[3])+(value[4]-value[3])+(value[3]-value[2])+(value[3]-value[1])

=value[5]+value[4]-value[2]-value[1]

通过这样可以发现这个规律:sum[i][j]=sum[i][j-1]+value[j]-value[(i+j)/2]

所以这样就把建在1到V个村庄的P个邮局这个问题拆开了,知道了某部分建立一个邮局的最优解,之后就是dp推导。

用dp[i][j]表示从1到j个村庄建立第i个邮局时的最优解,则有dp[i][j]=min(dp[i][j],dp[i-1][k-1]+sum[k][j]);

上面的推导公式表示将1到j个村庄建立i个邮局分成两部分,一部分是1到k-1个村庄建立i-1个邮局的最优解,另一部分是k到j建立一个邮局的最优解,不断从1到j枚举k的取值,选出最小的值即是最优解。

做这道题有一个问题当时想不出来是起始值不知道怎么确定,现在总结的话,真是猪头啊光速小子。初始值不就是dp[1][i]=sum[1][i]啊。当时居然完全没思路想不出来。。。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int V,P;
int value[305]; int sum[305][305];
int dp[35][305]; int main()
{
int i,j,k;
memset(sum,0,sizeof(sum));
for(i=0;i<=30;i++)
{
for(j=0;i<=300;j++)
{
dp[i][j]=10000;
}
}
cin>>V>>P; value[0]=0;
for(i=1;i<=V;i++)
{
cin>>value[i];
}
sum[1][2]=value[2]-value[1];
for(i=1;i<=V;i++)
{
for(j=i+1;j<=V;j++)
{
sum[i][j]=sum[i][j-1]+value[j]-value[(i+j)/2];
}
} for(i=1;i<=V;i++)//!!!!起始值
{
dp[1][i]=sum[1][i];
} for(i=2;i<=P;i++)
{
for(j=i;j<=V;j++)
{
for(k=1;k<=j;k++)
{
dp[i][j]=min(dp[i][j],dp[i-1][k-1]+sum[k][j]);
}
}
}
cout<<dp[P][V]<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1160:Post Office 邮局经典DP的更多相关文章

  1. poj 1160 Post Office 【区间dp】

    <题目链接> 转载于:>>> 题目大意: 一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...

  2. POJ 1160 Post Office(区间DP)

    Description There is a straight highway with villages alongside the highway. The highway is represen ...

  3. poj 1160 Post Office (间隔DP)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Desc ...

  4. [IOI 2000]POJ 1160 Post Office

    Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...

  5. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  6. POJ 1160 Post Office (四边形不等式优化DP)

    题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...

  7. POJ.1160.Post Office(DP 四边形不等式)

    题目链接 \(Description\) 一条直线上有n个村庄,位置各不相同.选择p个村庄建邮局,求每个村庄到最近邮局的距离之和的最小值. \(Solution\) 先考虑在\([l,r]\)建一个邮 ...

  8. poj1161Post Office【经典dp】

    题目:poj1161Post Officeid=1160" target="_blank">点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面 ...

  9. POJ 1160 Post Office (动态规划)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Desc ...

随机推荐

  1. hue中访问hdfs报错

    在hue中访问hdfs报错: Cannot access: /. Note: you are a Hue admin but not a HDFS superuser, "hdfs" ...

  2. 使用nginx做反向代理来访问tomcat服务器

    本次记录的是使用nginx来做一个反向代理来访问tomcat服务器.简单的来说就是使用nginx做为一个中间件,来分发客户端的请求,将这些请求分发到对应的合适的服务器上来完成请求及响应. 第一步:安装 ...

  3. DB2的简单操作

    转 最近在看db2,边读边写了一些,记下来,虽然写的乱七八糟.以备后用. 这些都写的很简单.我觉得也算是一些简单的操作吧,有些也是摘自别人的blog具体是引用哪的就不太记得了. 一.DB2两种注释写法 ...

  4. ubuntu 新建用户后 不能使用TAB键、上下键,命令行不显示当前路径的解决

    因默认ubuntu创建的普通帐号,默认shell为/bin/sh,而这不支持tab等键的,所以将「指定用户」帐号的shell改为/bin/bash就可以了. 1.查看当前的shell:# echo $ ...

  5. Day4-T1

    原题目 Hades 与 Dionysus 在狂饮后玩起了多米诺骨牌的小游戏. 现在桌上有 N 块多米诺骨牌,每块多米诺骨牌上半部分和下半部分上都有一个整数.每次翻转可让 一块多米诺骨牌上下翻转,即上下 ...

  6. 06--Java--Scanner类读入控制台

    Scanner类读入控制台 1.什么是Scanner类 Scanner类是java中从控制台读入用户输入的类 import java.util.Scanner; public class a_Lear ...

  7. Elasticsearch全文搜索引擎-PHP使用教程。

    1.声明依赖关系:         比方说,你的项目中需要一个php版的elasticsearch框架.为了将它添加到你的项目中(下载),你所需要做的就是创建一个 composer.json 文件,其 ...

  8. 021-PHP常用的数值类型判断函数

    <?php //判断数组 $colors = array("red", "blue", "green"); if(is_array($ ...

  9. 135-PHP final类和方法都是不可被继承或覆盖的

    <?php final class final_class{ //定义final修饰的类 } class myclass extends final_class{ //试图继承final修饰的类 ...

  10. (21)Laplance

    这个算法还是用来进行边缘检测的 =============================== #include <opencv2/opencv.hpp> #include <ios ...