3
1
2016

### 【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个城市里建邮局，使所有城市到最近邮局的距离和最小。

【分析】

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

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

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

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]

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写反，只抄代码不思考怎么行?!

【代码】#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];
{
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
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

(输入验证码)
or Ctrl+Enter

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