您的位置:首页 > 其它

H - Post Office POJ - 1160

2017-09-09 23:51 399 查看
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

一开始看到这道题的时候直接想到背包,问题可转化为从左向右选取p个邮局,对每一个村庄进行决策是否选取为邮局,若选取为邮局,大可以往左边找一个已得到的p - 1个邮局所得最小值,然后再去得到当前选取村庄得到p个邮局时的最小值。

具体代码如下,由于O(pn^3),直接超时。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#define INF 0x3f3f3f3f
int pos[505], dp[505];
int main()
{
int v, p;
while (scanf("%d%d", &v, &p) != EOF) {
for (int i = 1; i <= v; ++i) {
scanf("%d", &pos[i]);
}
pos[0] = -INF;
dp[0] = INF;
for (int i = v - p + 1; i >= 1; i--) {
for (int j = i - 1; j >= 1; j--) {
dp[i] = dp[i] + pos[i] - pos[j];
}
for (int j = i; j <= v; ++j) {
dp[i] = dp[i] + pos[j] - pos[i];
}
}
for (int i = 2; i <= p; ++i) {//放置p个邮站
for (int j = v - p + i; j >= i; j--) {//第i个邮站能放置的位置pos[j]
for (int k = j - 1; k >= i - 1; --k) {//枚举可放的上一个邮站位置
int t1 = 0, t2 = dp[k];
for (int l = k + 1; l <= v; ++l) {//对当前选取的邮站
if (l <= j)
t1 += std::min(pos[l] - pos[k], pos[j] - pos[l]);
else
t1 += std::min(pos[l] - pos[k], pos[l] - pos[j]);
t2 = t2 - (pos[l] - pos[k]);
}
if (k != j - 1)
dp[j] = std::min(dp[j], t1 + t2);
else
dp[j] = t1 + t2;
}
//printf("i = %d j = %d dp[j] = %d\n", i, j, dp[j]);
}
}
int Min = dp[p];
for (int i = 1; i <= v; ++i)
Min = std::min(Min, dp[i]);
printf("%d\n", Min);
}
return 0;


于是可以想到去进行优化,显然选取第i个村庄作为第j个邮局时,可在前面找第k个村庄,作为第j - 1个邮局,这时候当前选取第i个村庄,考虑一下,前k - 1个村庄最近的邮局必定不会是第j个邮局,那么接下来考虑k村庄后面的村庄,显然,第i个村庄右边的村庄,在未加入其他邮局的情况下,其最近的邮局必定是第i个村庄所在的位置,接下来就是(k,i)这些村庄的最短的距离和了,显然这是可以预处理出来的,预处理时间复杂度为O(n^3),dp时时间复杂度为O(pn^2);具体实现可看代码,都已经注释过了。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#define INF 0x3f3f3f3f
int pos[305], dp[305], a[305], b[305][305];
int main()
{
int v, p;
while (scanf("%d%d", &v, &p) != EOF) {
for (int i = 1; i <= v; ++i) {
scanf("%d", &pos[i]);
}
//memset(dp, 0, sizeof(dp));
//memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
pos[0] = -INF;
dp[0] = INF;
for (int i = v - p + 1; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
dp[i] = dp[i] + pos[i] - pos[j];
}
}
a[v] = 0;
for (int i = v - 1; i >= p; --i) {//后缀和
a[i] = a[i + 1] + (pos[i + 1] - pos[i]) * (v - i);
//printf("i = %d a[i] = %d\n", i, a[i]);
}
for (int i = 1; i <= v; ++i) {//i,j村庄建设为邮局后,其间的村庄最小距离和。
for (int j = i + 1; j <= v; ++j) {
for (int k = i + 1; k <= j - 1; ++k) {
b[i][j] = b[i][j] + std::min(pos[k] - pos[i],pos[j] - pos[k]);
}
}
}
for (int i = 2; i <= p; ++i) {//放置p个邮站
for (int j = v - p + i; j >= i; j--) {//第i个邮站能放置的位置pos[j]
for (int k = j - 1; k >= i - 1; --k) {//枚举可放的上一个邮站位置
if (k != j - 1) {
if (dp[j] > dp[k] + b[k][j]) {
//printf("i = %d j = %d dp[j] = %d k = %d\n", i, j, dp[j],k);
dp[j] = dp[k] + b[k][j];
}
}
else {
dp[j] = dp[k] + b[k][j];
//printf("i = %d j = %d dp[j] = %d k = %d\n", i, j, dp[j], k);
}
}
//printf("i = %d j = %d dp[j] = %d a[j] = %d\n", i, j, dp[j],a[j]);
}
}
int Min = dp[p] + a[p];
for (int i = p + 1; i <= v; ++i)
Min = std::min(Min, dp[i] + a[i]);
printf("%d\n", Min);
}
return 0;
}


注记:实际上在网上搜到的代码基本都是区间dp,我这个乱搞出来的思路绝对是对的,但可能并不是那么好用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: