/*
第一个四边形不等式优化题(虽然数据量小,普通的dp即可秒过)
写倒是好写,难在四边形不等式的证明上,不会证~
待会看证明去~
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int NN=310;
const int INF=0x7fffffff;
int n,m;
int s[NN],dp[NN][NN],w[NN][NN],p[NN][NN];
void get_w()//求在i~j号村庄间建一个邮局的最小距离和
{
for (int i=1; i<n; i++)
for (int j=i+1; j<=n; j++)
{
if ((i+j)%2) w[i][j]=s[j]-s[(i+j)/2]*2+s[i-1];
else w[i][j]=s[j]-s[(i+j)/2]-s[(i+j)/2-1]+s[i-1];
}
}
void get_dp()
{
for (int i=1; i<=n; i++)
{
dp[1][i]=w[1][i];
p[1][i]=0;
}
for (int c=2; c<=m; c++)
{
p[c][n+1]=n;
for (int i=n; i; i--)
{
int tmp=INF,k;
for (int j=p[c-1][i]; j<=p[c][i+1]; j++)
{
if (dp[c-1][j]+w[j+1][i]<tmp) tmp=dp[c-1][k=j]+w[j+1][i];
}
dp[c][i]=tmp;
p[c][i]=k;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
s[0]=0;
int x;
for (int i=1; i<=n; i++)
{
scanf("%d",&x);
s[i]=s[i-1]+x;
}
get_w();
get_dp();
printf("%d",dp[m][n]);
return 0;
}
POJ1160-四边形不等式优化
最新推荐文章于 2021-03-25 23:29:42 发布