POJ1160 Post Office (四边形不等式优化DP)

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1160 Post Office (四边形不等式优化DP)相关的知识,希望对你有一定的参考价值。

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

n个绿色点选m个染成红点,使得每个点到最近的红点距离和最小;

 

划分区间的时候,c[i][j]的为i到j染一个点的最优解(很好的思想)。

 

暴力的DP:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int inf=1000000000;
const int maxn=330;
int sum[maxn],a[maxn],dp[maxn][33];
int c[maxn][maxn];
int main()
{
     int n,m,i,j,k;
     scanf("%d%d",&n,&m);
     for(i=1;i<=n;i++) scanf("%d",&a[i]);
     for(i=1;i<=n;i++)
      for(j=1;j<=m;j++) dp[i][j]=inf;
      
     sort(a+1,a+n+1);
     for(i=1;i<=n;i++)
       for(j=i+1;j<=n;j++){
           int mid=(i+j)/2;
           for(k=i;k<=j;k++)
             c[i][j]+=abs(a[k]-a[mid]);
       }
     for(i=1;i<=n;i++)  dp[i][1]=c[1][i];
     for(i=2;i<=n;i++)
       for(j=2;j<=m;j++){
         for(k=1;k<i;k++)
            dp[i][j]=min(dp[i][j],dp[k][j-1]+c[k+1][i]);
     }
     printf("%d\n",dp[n][m]);
     return 0;
}

优化的DP:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int inf=1000000000;
const int maxn=330;
int sum[maxn],a[maxn],dp[33][maxn];
int c[maxn][maxn],s[maxn][maxn];
int main()
{
     int n,m,i,j,k;
     scanf("%d%d",&n,&m);
     for(i=1;i<=n;i++) scanf("%d",&a[i]);
     for(i=1;i<=m;i++)
      for(j=1;j<=n;j++) dp[i][j]=inf;
      
     sort(a+1,a+n+1);
     
     for(i=1;i<=n;i++)
       for(j=i;j<=n;j++){
           int mid=(i+j)/2;
           for(k=i;k<=j;k++)
             c[i][j]+=abs(a[k]-a[mid]);
       }
       
     for(i=1;i<=n;i++)  dp[1][i]=c[1][i];
       for(i=2;i<=m;i++)  {  
            s[i][n+1]=n;  
            for(j=n;j>=1;j--)  {
                for(k=s[i-1][j];k<=s[i][j+1];k++)  {  
                    if(dp[i][j]>dp[i-1][k]+c[k+1][j]){
                        s[i][j] = k;  
                        dp[i][j]=dp[i-1][k]+c[k+1][j];  
                    }
                }  
            }  
        }  
     printf("%d\n",dp[m][n]);
     return 0;
}

 

以上是关于POJ1160 Post Office (四边形不等式优化DP)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1160 Post Office (四边形不等式优化DP)

四边形不等式POJ1160[IOI2000]-Post Office

题解POJ1160Post Office

IOI2000 Post Office (POJ1160)

pku 1160 Post Office 四边形不等式优化 经典DP

POJ1160 Post Office[序列DP]