zoukankan      html  css  js  c++  java
  • poj1160 Post Office

    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

    题目大意:
    给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。

    当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的

    所以我们令f[i][j]表示前i个村庄,j个邮局的最小值

    直接枚举左端点,将邮局放在中间

    放邮局的代价用n^2直接预处理

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 typedef long long lol;
     8 lol f[305][305],x[305],inf,c[305][305],ans;
     9 int n,m;
    10 int main()
    11 {int i,j,k;
    12   cin>>n>>m;
    13   for (i=1;i<=n;i++)
    14     scanf("%lld",&x[i]);
    15   memset(f,127/3,sizeof(f));
    16   inf=f[0][0];
    17   f[0][0]=0;
    18   for (i=1;i<=n;i++)
    19     {
    20       for (j=1;j<=i;j++)
    21     {
    22       int pos=(i+j)/2;
    23       for (k=j;k<=i;k++)
    24         {
    25           c[i][j]+=abs(x[pos]-x[k]);
    26         }
    27     }
    28     }
    29   for (i=1;i<=n;i++)
    30     {
    31       for (j=1;j<=m;j++)
    32     {
    33       for (k=1;k<=i;k++)
    34         {
    35           f[i][j]=min(f[i][j],f[k-1][j-1]+c[i][k]);
    36         }
    37     }
    38     }
    39   ans=inf;
    40   for (i=1;i<=m;i++)
    41     ans=min(ans,f[n][i]);
    42   cout<<ans;
    43 }

    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


    题目大意:
    给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。

    题目思路:
    dp想法。
    当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的。当有v个邮局的时候,在前v个村庄中建立j个邮局的最短距离,是在前 i个村庄中建立j-1个邮局的最短距离与在j+1到第i个邮局建立一个邮局的最短距离的和。建立一个邮局的答案可以预先处理。

    附上代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int main(){
        int v, p, a[1005]={0}, dp[305][50], sum[305][305];
        cin >> v >> p;
        memset( sum, 0, sizeof(sum) );
        for( int i=0 ; i<v ; i++ ){
            for( int j=0 ; j<=p ; j++ )
                dp[i][j] = 1<<29;
        }
        for( int i=0 ; i<v ; i++ ){
            scanf( "%d", &a[i] );
        }
        for( int i=0 ; i<v ; i++ ){
            for( int j=i+1 ; j<v ; j++ ){
                int pos = (i+j)/2;
                for( int k=i ; k<=j ; k++ )
                    sum[i][j] += abs( a[k]-a[pos] );
            }
        }
        for( int i=0 ; i<v ; i++ ) dp[i][1] = sum[0][i];
        for( int i=0 ; i<v ; i++ ){
            for( int j=2 ; j<=p ; j++ ){
                for( int k=0 ; k<=i ; k++ ){
                    dp[i][j] = min( dp[k][j-1]+sum[k+1][i], dp[i][j] );
                }
            }   
        }
        cout << dp[v-1][p] << endl;
        return 0;
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
     
    0
    0
     
     
    查看评论

      暂无评论

     
    * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
     
     
     
     
    个人资料
     
     
      • 访问:3259次
      • 积分:112
      • 等级:
      • 排名:千里之外
      • 原创:8篇
      • 转载:0篇
      • 译文:0篇
      • 评论:1条

    文章搜索

  • 相关阅读:
    Shell for
    rsync 目录 斜杠
    shell local
    linux secureCRT utf-8编码显示
    eclipse maven 项目不显示 target目录
    如何打印身份证实际大小
    linux 去掉 ^M
    hibernate 之 集合映射中list映射
    hibernate 之 复合主键映射
    hibernate 之 组件映射
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7723624.html
Copyright © 2011-2022 走看看