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

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个邮局。求全部村庄到它近期的邮局的距离和。村庄在一条直线上。邮局建在村庄上。

思路:首先求出在连续的几个村庄上建立一个邮局的最短距离,用数组dis[i][j]表示在第i个村庄和第j个村庄之间建一个邮局的最短距。

dis[i][j]=dis[i][j-1]+x[j]-x[(i+j)/2]; (村庄位置为x[i])

用数组dp[i][j]表示在前i个村庄中建立j个邮局的最小距离。即在前k(k<i)个村庄建立j-1个邮局,在k+1到j个村庄建立一个邮局。

dp[i][j]=min(dp[i][j],dp[k][j-1]+dis[k+1][i])

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 305
const int inf=0x3fffffff;
int dp[N][35]; //在前i个村庄中建立j个邮局的最小耗费
int dis[N][N];//dis[i][j]:第i个村庄到第j个村庄建一个邮局的最短距离
int x[N]; //村庄位置
int main()
{
int v,p,i,j,k;
while(scanf("%d%d",&v,&p)!=-1)
{
for(i=1;i<=v;i++)
scanf("%d",&x[i]);
//memset(dis,0,sizeof(dis));
for(i=1;i<=v;i++)
{
for(j=i+1;j<=v;j++)
{
dis[i][j]=dis[i][j-1]+x[j]-x[(i+j)/2];
}
}
for(i=1;i<=v;i++)
{
dp[i][i]=0; //一个村庄一个邮局距离为零
dp[i][1]=dis[1][i]; //前i个村庄建立一个邮局
}
for(j=2;j<=p;j++)
{
for(i=j+1;i<=v;i++)
{
dp[i][j]=inf;
for(k=j-1;k<i;k++)
{
dp[i][j]=min(dp[i][j],dp[k][j-1]+dis[k+1][i]);
}
}
}
printf("%d\n",dp[v][p]);
}
return 0;
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj 1160 Post Office (间隔DP)的更多相关文章

  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)

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

  4. poj 1160 Post Office 【区间dp】

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

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

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

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

    d(i, j)表示用i个邮局覆盖前j个村庄所需的最小花费 则有状态转移方程:d(i, j) = min{ d(i-1, k) + w(k+1, j) } 其中w(i, j)的值是可以预处理出来的. 下 ...

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

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

  8. [IOI 2000]POJ 1160 Post Office

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

  9. POJ 1160 Post Office

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

随机推荐

  1. 同一路由器不同vlan之间的通信(一)

    还是废话不多说,第一步,看拓扑图. 先把pc上的ip都配好.開始设置 switch0: >en >conf t >vlan 2 >exit >int fa 0/1 > ...

  2. 【原创】leetCodeOj --- Jump Game II 解题报告

    原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...

  3. 对XSD schema文件中elementFormDefault属性的理解

    Schema中的elementFormDefault elementFormDefault取值:qualified 或者 unqualified 在http://www.velocityreviews ...

  4. BZOJ3362 [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    标题效果:自脑补. 思维:与维护两个维度和可设置为检查右. 注意,标题给予一堆关系的.我们应该加入两对关系. Code: #include <cstdio> #include <cs ...

  5. leetcode第一刷_Minimum Path Sum

    能够用递归简洁的写出,可是会超时. dp嘛.这个问题须要从后往前算,最右下角的小规模是已知的,边界也非常明显,是最后一行和最后一列,行走方向的限制决定了这些位置的走法是唯一的,能够先算出来.然后不断的 ...

  6. 配置Tomcat出现Unsupported major.minor version 51.0

    在配置tomcat时,配置好jdk1.6,下载的tomcat8.0,结果执行start-up.bat,总是一闪而过,网上查了大量的资料,都说是可能是jdk没配置好,但实际上jdk的环境变量设置正常,后 ...

  7. ORACLE 实验一

    实验一:数据定义 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.熟悉Oracle的client配置: 2.掌握SQL Plus的使用: 3.掌握SQL模式定义语句,定义相关的表 ...

  8. 复制(1)——SQLServer 复制简介

    原文:复制(1)--SQLServer 复制简介 前言: SQLServer的复制技术最少从SQLServer2000时代已经出现,当初是为了分布式计算,不是为了高可用.但是到了今天,复制也成为了一种 ...

  9. Swing JDialog监听回车键

    在做项目时,发现在JDialog中,直接通过addKeyListener来监听回车键不起作用,无法监听到回车键,后面在网上查了些资料,终于解决了.方法如下: KeyStroke stroke = Ke ...

  10. Servlet(五岁以下儿童)web.xml一些常用的配置

    (1)lode-on-startup,这Servlet该项目启动时它将被称为(从主要的电话init办法,为了安全起见,一般不应为Servlet建立URL制图).一些数据通常被用作前处理,或使用多线程建 ...