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

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

Source

题目大意:

有n个村庄,m个邮局。每一个村庄的位置坐标告诉你,如今要将m个邮局设立在这n个村庄里面,问你最小花费是多少?花费为每一个村庄到近期的邮局的距离和。

解题思路:

dp[i][j] 记录 i个邮局 j个村庄的最小花费,cost[k+1][j]。记录在k+1号村庄到 j 号村庄设立一个邮局的最小花费。

那么:dp[i][j]=min { dp[i][k]+cost[k+1][j] }

最后输出dp[m][n]就可以。

可是在k+1号村庄到 j 号村庄设立一个邮局的最小花费是多少呢?答案:中位数。设置在中间那个村庄就可以。

解题代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=310;
const int maxm=40;
int cost[maxn][maxn],dp[maxm][maxn],a[maxn];
int n,m,s[maxn][maxn]; void input(){
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
cost[i][j]=0;
int mid=(i+j)/2;
for(int k=i;k<=j;k++){
cost[i][j]+=abs(a[k]-a[mid]);
}
}
}
} int DP(int c,int r){
if(dp[c][r]!=-1) return dp[c][r];
if(c==1) return cost[1][r];
int ans=(1<<30);
for(int i=1;i<r;i++){
int tmp=DP(c-1,i)+cost[i+1][r];
if(tmp<ans) ans=tmp;
}
return dp[c][r]=ans;
} void solve(){
cout<<DP(m,n)<<endl;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
input();
solve();
}
return 0;
}

POJ 1160 Post Office (动态规划)的更多相关文章

  1. POJ 1160 Post Office(区间DP)

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

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

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

  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)

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

  6. POJ 1160 Post Office

    题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...

  7. poj 1160 Post Office 【区间dp】

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

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

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

  9. POJ 1160 DP

    题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...

随机推荐

  1. Android - Mac系统Android程序位置

    Mac系统Android程序位置 本文地址: http://blog.csdn.net/caroline_wendy Mac系统是类Unix系统.Android程序直接安装至目录.能够使用" ...

  2. UVALive 3989 Ladies&#39; Choice

    经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format:  ...

  3. linux下ip命令用法

    配置数据转发,可以通过 1.路由转发即用用路由器实现: 2.使用NAT转发: 简单的说: 路由表内的信息只是指定数据包在路由器内的下一个去处.并不能改变数据包本身的地址信息.即它只是“换条路而已,目的 ...

  4. iTextSharp

    iTextSharp 116毫秒处理6G的文件   前言: 有一家印刷企业专为米兰新娘,微微新娘,金夫人这样的影楼印刷婚纱相册.通过一个B2B销售终端软件,把影楼的相片上传到印刷公司的服务器,服务器对 ...

  5. CSS鼠标样式

    1.缺省方式(箭头形状): cursor:default; 2.手型 cursor: pointer;   //通用的cursor: hand;     //为了兼容ie老版本,可以同时写上

  6. Silk Icons —— 再来 700 个免费小图标

    http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5NzM0MjcyMQ==&appmsgid=10000977&itemidx=2&am ...

  7. Android组件:Fragment切换后保存状态

    之前写的第一篇Fragment实例,和大多数人一开始学的一样,都是通过FragmentTransaction的replace方法来实现,replace方法相当于先移除remove()原来所有已存在的f ...

  8. Oracle、DB2、MySql、SQLServer JDBC驱动

    四种数据库JDBC驱动,还列出了连接的Class驱动名和Url Pattern,DB2包括Type 2.Type 3和Type 4三种模式.注意驱动包名称的大小写. Oralce连接驱动包名和URL ...

  9. CSS三角形制作样式

    .triangle{ display: block; height: 0; position: absolute; width: 0; border: 9px solid; border-color: ...

  10. C# - 数据库存取图片

    1.创建数据表 CREATE TABLE Tb_pic ( ID int primary key identity(1, 1) not null, PictureBox varchar(max) ) ...