Poj-1160 Post Office(经典dp

Post Office
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15258 Accepted: 8266

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


题目大意:对于一条直线上的v个村子,要建造p个邮局,邮局只能建在村子里,给定了村子的位置,求所有村子到达邮局的距离之和最小是多少。

方法:

用dp[i][j]表示前i个村子建造j个邮局的最优解,那么最终答案即为dp[v][p].

用sum[i][j]来表示第i个村子到第j个村子建造1个邮局的最优解,那么sum[i][j]=sum[i][j-1]+(map[j]-map[(i+j)/2]);

因为只需建造1个邮局时一定是建在中点最优(偶数个的话则中间两个点都一样),

若之前有偶数个村子(i->j-1),在此基础上再加一个j,变成奇数个村子,之前的中点(较靠后的那个)恰好也是现在的中点,那么只需再加上j到中点的距离即可

若之前有奇数个村子,同理~

至此,dp[i][j]=min{dp[i][j],dp[k][j-1]+sum[k+1][i]} (j-1<=k<i)


代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int lin=305;
int map[lin],sum[lin][lin],dp[lin][35];
bool vis[lin][35];

inline int smi(int x,int y)
{
	if(x<y)	return x;
	return y;
}
int f(int i,int j)
{
	if(vis[i][j])	return dp[i][j];
	vis[i][j]=true;

	int tem=999999999;
	for(int k=j-1;k<i;++k)
	{
		tem=smi(tem,f(k,j-1)+sum[k+1][i]);
	}
	dp[i][j]=tem;
	return dp[i][j];
}

int main()
{
	int v,p;
	while(cin>>v>>p)
	{
		memset(map,0,sizeof(map));
		for(int i=1;i<=v;++i)
		{
			scanf("%d",&map[i]);
		}

		memset(sum,0,sizeof(sum));
		for(int i=1;i<=v;++i)
		{
			for(int j=i+1;j<=v;++j)
			{
				sum[i][j]=sum[i][j-1]+map[j]-map[(i+j)/2];
			}
		}

		memset(vis,0,sizeof(vis));
		for(int i=1;i<=v;++i)
		{
			dp[i][1]=sum[1][i];
			vis[i][1]=true;
		}

		cout<<f(v,p)<<endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值