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. XML(三)

     使用 XSLT 显示 XML -------------------------------------------------------------------------------- 通 ...

  2. eclipse如何设置成保护眼的背景色

    链接地址:http://jingyan.baidu.com/article/2a138328b5d9ea074a134fc7.html 长时间的使用eclipse开发会很累吧  设置一个保护眼睛的豆沙 ...

  3. 纠错记录(Could not open the editor: Android XML Editor cannot process this input.)

    Eclipse无法打开xml文件 preference->general->edit->file association->file types->选择.xml,然后在a ...

  4. CSS简介和CSS选择器

    <pre name="code" class="html">Css 简洁 css 选择器 标签选择器 类选择器 ID选择器 Css 层叠样式表 Ca ...

  5. grunt getHTML

    var Base = require( "../common/base" ) , HandlerBase = require( "../common/handlerBas ...

  6. 怎样建立一个bower私库

    本教程适用于centos 安装之前 检查nodejs 假设没安装nodejs依照下面步骤安装 $ su - $ yum install openssl-devel $ cd /usr/local/sr ...

  7. HTML的表单元�

    HTML的表单元素 表单元素是同意用户在表单中(比方:文本域,下拉列表,单选框,复选框等等)输入信息的元素 表单标签 文本域(Text Fields) 当用户要在表单中键入字母,数字等内容时,就会用到 ...

  8. HDOJ 5088 Revenge of Nim II 位运算

    位运算.. .. Revenge of Nim II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. opencv视频播放

    在一个界面上显示一张图片.是一件非常easy的事情,但说到要显示视频.刚開始学习的人可能不知道怎么处理,事实上,一般来说能够理解为视频就是图片以人眼察觉不到的速度高速更新. 曾经用摄像头採集视频显示在 ...

  10. iostat查看io情况(监控Linux的8种方式)

    查看TPS和吞吐量信息[root@controller ~]#iostat -d -k 1 10Device:         tps    kB_read/s    kB_wrtn/s    kB_ ...