3
1
2016
1

【IOI2000】【poj1160】【四边形不等式优化dp】Post Office

Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16454   Accepted: 8915

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


【题意】给定n个城市,在m个城市里建邮局,使所有城市到最近邮局的距离和最小。

 


【分析】

朴素的dp方程:f[i][j]表示前j个村庄建i个邮局的最小代价。 

w[i][j]表示ij之间建一个邮局的最小代价。

以邮局数目为阶段转移。

 

f[i][j]=min(f[i-1][k]+w[k+1][j])

Getw计算w数组:<似乎有些问题??>

容易想到,对于每一段村庄区间,邮局建在中间村庄的距离和最小,如果不是建在中间,则必然有重复走的路程,可以通过特殊例子看出。

若这一段区间内有奇数个村庄,则邮局建在中间的村庄,路程之和为区间长度

若有偶数个村庄,则邮局建在中间的两个村庄任一均可,路程之和为区间长度+中间两村庄距离

递推至后一位置时,中间的村庄不变,由此:w[i][j]=w[i][j-1]+p[j]-p[(i+j)>>1];

 

四边形不等式优化:(%%%Regina~

如果w[i][j]满足四边形不等式即w[i][j]+w[i'][j']<=w[i][j']+w[i'][j]  (i<=i'<=j<=j')

那么f[i][j]也满足四边形不等式,

f[i][j]=f[i-1][k]+w[k+1][j]f[i][j]k的时候取到最优值,记s[i][j]=k,则s[i][j]满足单调性:

s[i-1][j]<=s[i][j]<=s[i][j+1]

于是k就不需要从1开始枚举了,从s[i-1][j]s[i][j+1]枚举即可。

 

几个细节:

1、数组赋初值、边界值

w[i][i]=0;

Memset(f,127,sizeof(f));f[1][i]=w[1][i];

s[1][i]=0;s[i][n+1]=n;//这个具体原因还没想十分清楚

2倒推可以减少可行状态,降低复杂度<不太明白>

3WA了好几发..

m,n写反,只抄代码不思考怎么行?!

数组大小M,N写反=_=.注意w300*300

 


【代码】#include<cstdio>

#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define inf 1<<30
using namespace std;
const int M=500,N=50;
typedef long long LL;
int m,n;
int x[M],f[N][M],s[N][M],w[M][M];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void Getw()
{
	F(i,1,m){
		w[i][i]=0;
		F(j,i+1,m)	w[i][j]=w[i][j-1]+x[j]-x[(i+j)>>1];
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("poj1160.txt", "r", stdin);
	freopen("post office .txt", "w", stdout);
	#endif
			m=read();n=read();
			F(i,1,m) x[i]=read();
			Getw();//www~
			memset(f,127,sizeof(f));
			F(i,1,m) {
				f[1][i]=w[1][i];//不建造邮局时
				s[1][i]=0;
			}
			F(i,2,n){//以邮局数目为状态进行转移
				s[i][m+1]=m;
				D(j,m,i)
					F(k,s[i-1][j],s[i][j+1])
						if(f[i-1][k]+w[k+1][j]<f[i][j])
							s[i][j]=k,f[i][j]=f[i-1][k]+w[k+1][j];
			}
		printf("%d\n",f[n][m]);
		return 0;
}

 


【反思】time:20:21-21:11

说来这道题与仓库运输的情景还有些类似,邮局可以建造多个,不同的是建造不需费用,运输可以双向向最近的邮局。

跑的速度还行..

 

Category: 动态规划 | Tags: 动归优化 动态规划 四边形不等式优化dp | Read Count: 1689
Avatar_small
Mitchell 说:
2019年5月02日 18:40

Details of the program with input and output are very helpful especially beginners will praise you with read of this share. Students submit their research work with hire of writing dissertation services in writers handle their writing requirements.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com