/*
Post Office
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6697 Accepted: 3531
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
*/
#include <iostream>
using namespace std;
int village[303];
int value[33][303];
int cost[303][303];
int V,P;
void dp()
{
int i,j,tmp,k;
for(j = 1;j <= V;j++)
{
value[1][j] = cost[1][j];
}
for(i = 2;i <= P;i++)
{
for(j = i;j <= V;j++)
{
tmp = 65535;
for(k = i - 1;k < j;k++)
{
if(tmp > value[i - 1][k] + cost[k + 1][j])
{
tmp = value[i - 1][k] + cost[k + 1][j];
}
}
value[i][j] = tmp;
}
}
return;
}
int main(void)
{
int i,j,k;
cin>>V>>P;
for(i = 1;i <= V;i++)
{
cin>>village[i];
}
for(i = 1;i <= V;i++)
{
for(j = 1;j <= V;j++)
{
cost[i][j] = 0;
for(k = i;k <= (i + j) / 2;k++)
{
cost[i][j] = cost[i][j] + village[(i + j) / 2] - village[k];
}
for(k = (i + j) / 2 + 1;k <= j;k++)
{
cost[i][j] = cost[i][j] + village[k] - village[(i + j) / 2];
}
}
}
dp();
cout<<value[P][V]<<endl;
return 0;
}
这个题目要抓对DP的切入点,就是——前i个邮局控制前j个村庄时的最少总距离的值(value[i][j])
cost[i][j]是指某个邮局控制了第i个村庄到第j个村庄之间的所有村庄(包括i和j)时的最少距离的值
首先对cost[i][j]初始化:
for(i = 1;i <= V;i++)
{
for(j = 1;j <= V;j++)
{
cost[i][j] = 0;
for(k = i;k <= (i + j) / 2;k++)
{
cost[i][j] = cost[i][j] + village[(i + j) / 2] - village[k];
}
for(k = (i + j) / 2 + 1;k <= j;k++)
{
cost[i][j] = cost[i][j] + village[k] - village[(i + j) / 2];
}
}
}
(i + j) / 2的意思是当某个邮局控制了第i个村庄到第j个村庄之间的所有村庄(包括i和j)时,若要是这段的总距离最少那么这个邮局就必定是在第(i + j) / 2这个村庄里(这里我大概知道为什么但是说不清楚。。。。。)
接下来就是DP了:
for(i = 2;i <= P;i++)
{
for(j = i;j <= V;j++)
{
tmp = 65535;
for(k = i - 1;k < j;k++)
{
if(tmp > value[i - 1][k] + cost[k + 1][j])
{
tmp = value[i - 1][k] + cost[k + 1][j];
}
}
value[i][j] = tmp;
}
}
本文介绍了一种使用动态规划解决邮局选址问题的方法,旨在找到覆盖所有村庄的最少总距离。通过初始化cost数组来计算单一邮局覆盖村庄的最短距离,并采用DP策略逐步求解最优解。
1721

被折叠的 条评论
为什么被折叠?



