邮局选址问题
#include <bits/stdc++.h>
#include<stdlib.h>
using namespace std;
int n,m,a[3005],mk[3005][3005];
long long dp[305][3005],w[3005][3005];
int main ()
{
cout<<"请分别输入村庄和邮局个数:"<<endl;
scanf("%d%d",&n,&m);
cout<<"请输入村庄的坐标:"<<endl;
for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
sort(a + 1,a + n + 1);
for (int i = 1;i <= n;i++)
for (int j = i + 1;j <= n;j++)
w[i][j] = w[i][j - 1] + a[j] - a[(i + j) / 2];
for (int i = 1;i <= n;i++) dp[1][i] = w[1][i],mk[1][i] = 0;
for (int i = 2;i <= m;i++)
{
mk[i][n + 1] = n;
for (int j = n;j > i;j--)
{
dp[i][j] = 1ll << 60;
for (int k = mk[i - 1][j];k <= mk[i][j + 1];k++)
if (dp[i][j] > dp[i - 1][k] + w[k + 1][j])
dp[i][j] = dp[i - 1][k] + w[k + 1][j],mk[i][j] = k;
}
}
printf("最小距离和为:%lld\n",dp[m][n]);
system("pause");
return 0;
}