# [POJ 1160] Post Office【DP+四边形不等式优化】

• 2018-01-25
• 0
• 1

## Problem:

 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 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

IOI 2000

## Solution:

• cost[ i ][ i ] = 0 …… (1)
• cost[ i ][ j ] = cost[ i ][ j - 1 ] + X[ j ] - X[ ( i + j ) / 2 ], 其中 i < j ≤ V …… (2)
• dp[ i ][ j ] = min{ dp[ k ][ j - 1] + cost[ k + 1 ][ i ] }, 其中 1 ≤ k < i …… (3)

• (1) 式显然正确，(2) 式是由于邮局建在中位数处距离总和最小，可以画图证明。
• (3) 式的正确性并不显然，主要疑点在于转移时能否保证离前 k 个村庄最近的邮局均不变。
• 我们很容易发现，dp[ i ][ j ]  ≤ dp[ i ][ j - 1 ]
• 若转移时第 p ~ k 个村庄 (1 ≤ p ≤ k) 反而离新建的邮局更近，那么
• dp[ p ][ j - 1 ] + cost[ p + 1 ][ i ] < dp[ k ][ j - 1 ] + cost[ p + 1 ][ i ]
• 所以此时不可能更新 dp 而导致答案错误。

• dp[ i ][ j ] = min{ dp[ k ][ j - 1] + cost[ k + 1 ][ i ] }, 其中 s[i][j - 1] ≤ k ≤ s[i + 1][j]

## Code #1: O(V3P) [1064K, 16MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int V, P, X[310];
int dp[310][30], cost[310][310];

int main(){
scanf("%d%d", &V, &P);
for(register int i = 1; i <= V; i++) scanf("%d", X + i);
for(register int i = 1; i <= V; i++){
cost[i][i] = 0;
for(register int j = i + 1; j <= V; j++)
cost[i][j] = cost[i][j - 1] + X[j] - X[i + j >> 1];
}
memset(dp, 0x3f, sizeof(dp)), dp[0][0] = 0;
for(register int j = 1; j <= P; j++)
for(register int i = j; i <= V; i++)
for(register int k = 0; k < i; k++)
dp[i][j] = min(dp[i][j], dp[k][j - 1] + cost[k + 1][i]);
printf("%d\n", dp[V][P]);
return 0;
}
```

## Code #2: O(V2P) [1112K, 0MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int V, P, X[310];
int dp[310][35], cost[310][310];
int s[310][35];

int main(){
scanf("%d%d", &V, &P);
for(register int i = 1; i <= V; i++) scanf("%d", X + i);
for(register int i = 1; i <= V; i++){
cost[i][i] = 0;
for(register int j = i + 1; j <= V; j++)
cost[i][j] = cost[i][j - 1] + X[j] - X[i + j >> 1];
}
memset(dp, 0x3f, sizeof(dp));
for(register int i = 1; i <= V; i++){
dp[i][1] = cost[1][i];
s[i][1] = 1;
}
for(register int j = 1; j <= P; j++) s[V + 1][j] = V - 1;
for(register int j = 2; j <= P; j++)
for(register int i = V; i >= j; i--)  // Reversed order
for(register int k = s[i][j - 1]; k <= s[i + 1][j]; k++)
if(dp[k][j - 1] + cost[k + 1][i] < dp[i][j]){
dp[i][j] = dp[k][j - 1] + cost[k + 1][i];
s[i][j] = k;
}
printf("%d\n", dp[V][P]);
return 0;
}
```

#### 评论

• ###### bjz 2018-05-09 07:57回复

%%%%%%%%%%%%%

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai