1. 程式人生 > >POJ1160 Post Office (四邊形不等式優化DP)

POJ1160 Post Office (四邊形不等式優化DP)

single inpu cst class pac ios 不等式 lang rep

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)