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. GoJS最简单的实例

    复制如下内容保存到空白的.html文件中,用浏览器打开即可查看效果 <!DOCTYPE html> <html> <head> <meta charset=& ...

  2. R 《回归分析与线性统计模型》page119,4.2

    rm(list = ls()) library(openxlsx) library(MASS) data = read.xlsx("xiti_4.xlsx",sheet = 2) ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-inbox

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. G - Traffic

    vin is observing the cars at a crossroads. He finds that there are n cars running in the east-west d ...

  5. python基础数据类型--集合(set)

    python基础数据类型--集合(set) 集合是一个数学概念由一个或多个确定的元素所构成的整体叫做集合 集合中的三个特征 1.确定性(元素必须死可hash) 2.互异性(去重) 3.无序性(集合中的 ...

  6. P 1008 说反话

    转跳点:

  7. 《Java并发编程的艺术》并发编程的基础(四)

    一.线程简介 1.线程的概念 系统运行的最小单元 2.为何使用多线程 更好地利用系统资源(处理器多核心),提高响应速度. 3.线程的状态 NEW(创建状态) RUNABLE(运行状态,系统调度,争抢时 ...

  8. SharePreference的使用

    SharePreference 一般用于保存偏好设置,比如说我们设置里的条目 sharepreference使用步骤 1.拿到sharepreference //拿到share preference ...

  9. 十四、JavaScript之不同类型变量相加

    一.代码如下 二.效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  10. 七十七、SAP中数据库操作之多表联合查询

    一.我们看一下SFLIGHT表和SPFLI表,表结构如下 二.这2个表的数据如下 三.我们代码如下 四.多表联合查询结果如下