码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-11 18:57:08      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:ble   style   --   contain   好的   some   standard   std   string   

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)

标签:ble   style   --   contain   好的   some   standard   std   string   

原文地址:http://www.cnblogs.com/hua-dong/p/7819612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!