162:Post Office

总时间限制:
1000ms
内存限制:
65536kB

描述
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.

输入
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.
输出
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
样例输入

10 5
1 2 3 6 7 9 11 22 44 50

样例输出

9

译文:
有一条直通公路的村庄。公路被表示为一个整数轴,每个村庄的位置是用一个整数坐标来确定的。有在同一位置没有两村。两个位置之间的距离是其整数坐标差的绝对值。一个村庄,邮局也有相同的位置。建筑的邮局,其位置的选择应使每个村子和邮局之间的最小距离的总和。

你要写一个程序,给出了村庄的位置和邮局的数量,计算出每个村和它最近的邮局之间的距离最小的总和。

你输入程序从标准输入读取。第一行包含两个整数:第一个是村庄V的数目,1个= v=300,第二个是邮局P的数目,1个= p=< = 30,P = V,第二行是递增的V整数。这些V的整数是村庄的位置。每个位置x认为1≤x≤10000。

输出第一行包含一个整数,这是每个村子和邮局之间的距离的总和。样例输入
解题思路,这题用的是动态规划的解题思路,但是这题的状态转移方程不是太好找;
f[i][j]:表示从i个村庄中选取j个作为邮局的路径综合的最小值
mi[i][j]:表示从第i个村庄到第j个村庄的只建立一个邮局的路径的最小值;
可以很明显的得出若从第i个村庄到第j个村庄只选取一个作为邮局的话则选择第(i+j)/2个,2、下面考虑建立多个邮局的问题,可以这样将该问题拆分为若干子问题,在前i个村庄中建立j个邮局的最短距离,是在前k个村庄中建立j-1个邮局的最短距离与 在k+1到第i个邮局建立一个邮局的最短距离的和。而建立一个邮局我们在上面已经求出,则动态转移方程可得f[i][j]=min(f[i][j],f[k][j-1]+mi[k+1][j])
其中状态边界f[i][1]=mi[1][i];mi[i][j]=mi[i][j-1]+a[i]-a[(i+j)/2];

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1001],f[500][500],mi[500][500],i,j;
int main()
{
    cin>>n>>m;
        for(i=1;i<=n;i++)
       cin>>a[i];
      for(i=1;i<=n;i++)
          for(j=1;j<=m;j++)
                    f[i][j]=9999999;//赋为最大值
              for(i=1;i<=n;i++)
              {
                   for(j=i+1;j<=n;j++)
                    mi[i][j]=mi[i][j-1]+a[j]-a[(i+j)/2];//从第i个村庄到第j个村庄中只建一个邮局的状态转移方程
                    f[i][1]=mi[1][i];//状态转移方程的边界

              }

                 for(i=2;i<=n;i++)
                    for(j=2;j<=m;j++)
                        for(int k=j-1;k<i;k++)
                    f[i][j]=min(f[i][j],f[k][j-1]+mi[k+1][i]);
         cout<<f[n][m]<<endl;
         return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值