poj1160 Post Office

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

这题是区间dp经典,看了别人的代码,自己写出来了。可以先设两个数组sum[i][j]和dp[i][j],sum[i][j]表示第i和第j个村庄之间建一个邮局并且这些村庄的花费都和这个邮局计算得到的最小花费,dp[i][j]表示前i个村庄中间j个邮局并且这i个村庄的花费计算都与这些邮局有关的最小花费。

那么前i个村庄中建j个邮局的最小花费可以由前k个村庄中建i-1个邮局的最小花费加上第k个邮局到第i个邮局建一个邮局的最小花费转移过来,即dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);

这里的初始条件是dp[i][1]=sum[1][i].

当邮局数为1时,我们把邮局建在中间就行了,当邮局有多个时,就有sum[i][j]=sum[i][j-1]+pos[j]-pos[(i+j)/2]。

#include<stdio.h>
#include<string.h>
#define maxn 350
#define inf 88888888
int min(int a,int b){
	return a<b?a:b;
}
int pos[maxn],sum[maxn][maxn],dp[maxn][35];
int main()
{
	int n,m,i,j,k;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(dp,inf,sizeof(dp));
		memset(sum,0,sizeof(sum));
		for(i=1;i<=n;i++){
			scanf("%d",&pos[i]);
		}
		if(n==m){
			printf("0\n");continue;
		}
		for(i=1;i<=n-1;i++){
			for(j=i+1;j<=n;j++){
				sum[i][j]=sum[i][j-1]+pos[j]-pos[(i+j)/2];
			}
		}
		for(i=1;i<=n;i++){
			dp[i][1]=sum[1][i];
		}
		for(j=2;j<=m;j++){
			for(i=j+1;i<=n;i++){
				for(k=1;k<=i;k++){
					dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);
				}
			}
		}
		printf("%d\n",dp[n][m]);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-28 19:31:44

poj1160 Post Office的相关文章

poj1160 Post Office 四边形不等式

在一条直线上有n个村庄,选出m个村庄,在其中每个村庄建立一个邮局,要求每个村庄到最近邮局的距离和最小. f[i][j]:在前i个村庄中建立j个邮局的最小耗费 dis[i][j]:在第i个村庄到第j个村庄中建立1个邮局的最小耗费 那么就有转移方程:f[i][j] = min(f[i][j],f[k][j-1]+dis[k+1][i]) DP的边界状态即为f[i][1] = dis[1][i]; 所要求的结果即为f[n][m]; 然后就说说求dis数组的优化问题,可以假定有6个村庄,村庄的坐标已知分

POJ1160 Post Office[序列DP]

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18680   Accepted: 10075 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each vill

【DP|数学+预处理】POJ-1160 Post Office

Post Office Time Limit: 1000MS Memory Limit: 10000K 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 coord

POJ1160 Post Office (四边形不等式优化DP)

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 dista

HDU3480 Division

题目大意 将N个数分成M部分,使每部分的最大值与最小值平方差的和最小. 思路 首先肯定要将数列排序,每部分一定是取连续的一段,于是就有了方程 $\Large f(i,j)=min(f(i-1,k-1)+(a_j-a_k)^2)$ 其中$f(i,j)$表示前$j$个数分成$i$部分的最小值 解法一.四边形不等式优化 设$w(i,j)=(a_j-a_i)^2$ 方程变为$f(i,j)=min(f(i-1,k-1)+w(k,j))$ 很容易想到四边形不等式优化 证明w满足四边形不等式 $w(i,j)-

POJ1160 【Post Office】

POJ1160 [Post Office] 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 villag

IOI2000 Post Office (POJ1160)

前言 昨天XY讲课!讲到这题!还是IOI的题!不过据说00年的时候DP还不流行. 题面 http://poj.org/problem?id=1160 分析  § 1 中位数 首先我们考虑,若有x1 < x2 < ... < xn,则当∑abs(x - xi)最小时,x为x1 , x2 , ... , xn这n个数的中位数. 证明如下:我们把x1 , x2 , ... , xn看作数轴上n个点,我们先考虑两端,要使abs(x - x1) + abs(x - xn)最小,那么x必定在x1和x

题解【POJ1160】Post Office

[POJ1160]Post Office 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 village

【poj1160】 Post Office

http://poj.org/problem?id=1160 (题目链接) 题意 按照递增顺序给出一条直线上坐标互不相同的n个村庄,要求从中选择p个村庄建立邮局,每个村庄使用离它最近的那个邮局,使得所有村庄到各自所使用的邮局的距离总和最小. Solution 经典dp方程: 其中f[i][j]表示前j个村庄,放置i个邮局的最优方案.w[i][j]表示在i到j的村庄放置一个邮局,i~j的村庄到这个邮局的总距离.考虑如何求解w[i][j],因为只放置一个邮局,所以一定是放在最中间的那个点上,所以邮局