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. Hadoop源码解析之: TextInputFormat如何处理跨split的行

    我们知道hadoop将数据给到map进行处理前会使用InputFormat对数据进行两方面的预处理: 对输入数据进行切分,生成一组split,一个split会分发给一个mapper进行处理. 针对每个 ...

  2. 过渡到SSAS之一:简单模型认识

    本文主要是转载的,但有些地方,原作者没有说的够详细,我加以补充发到这里. --------------------------------------------------------------- ...

  3. 六、Nginx 防盗链

    盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载.尤其热门资源的盗链,对网站带宽的消耗非常大,本文通过nginx的配置指令location来实现简单的图片和其它类型文件的防盗链. ...

  4. 我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)

    作业二:三级菜单 三级菜单 可一次进入各个子菜单 思路: 这个题看似不难,难点在于三层循环的嵌套,我的思路就是通过flag的真假来控制每一层的循环的,简单来说就是就是通过给每一层循环一个单独的布尔变量 ...

  5. java--随机数的产生

    随机数产生的三种方法: 1.system.currentTimeMillis() public class Demo1{ public static void main(String[] args) ...

  6. cocos2dx-lua捕获用户touch事件的几种方式

    这里仅仅针对lua 1.为每一个关心的事件注冊回调函数 详细分为下面几种 1>单点触摸 注冊函数为 cc.Handler.EVENT_TOUCH_BEGAN      = 40 cc.Handl ...

  7. 开发板和centos服务器tftp传文件

    CentOS下使用TFTP向目标板传送文件http://www.linuxidc.com/Linux/2010-10/29218.htm 1.安装相关软件包 为了使主机支持TFTP,必须确保TFTP后 ...

  8. Core 中文文档

    ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序   原文:Your First ASP. ...

  9. 1030 - Image Is Everything

    Your new company is building a robot that can hold small lightweight objects. The robot will have th ...

  10. RHEL Server 6.3下MySQL5.5.25a源码安装

    OS:RHEL Server 6.3 MySQL:mysql-5.5.25a.tar.gz 相关依赖包: ncurses-5.9.tar.gz bison-2.5.tar.gz 安装MySQL 一.安 ...