zoukankan      html  css  js  c++  java
  • POJ——T 1160 Post Office

    http://poj.org/problem?id=1160

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 20218   Accepted: 10899

    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

    Source

     
     
    minx[i][j]表示在i到j个村庄中,放1个邮局的最小距离和、
       当 i 到 j 有三个村庄,a1, a2, a3 那么邮局一定选在a2,当有a1,a2,a3,a4时,选在a2,a3是等效的,增加a5时,选在a3,最优,
        以此类推,得到 minx[i][j] = minx[i][j-1]+x[j]-x[i+j>>1] )
    f[i][k]表示,从第1个村庄到第i个村庄放了k个邮局的最小距离和、
    那么 f[i] [k] =min{ f[j] [k-1]+minx[j+1][i] }(j<i)
     
     1 #include <cstring>
     2 #include <cstdio>
     3 
     4 inline void read(int &x)
     5 {
     6     x=0; register char ch=getchar();
     7     for(; ch>'9'||ch<'0'; ) ch=getchar();
     8     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
     9 }
    10 
    11 const int N(323);
    12 #define min(a,b) (a<b?a:b)
    13 int n,m,x[N],minx[N][N],f[N][N];
    14 
    15 int Presist()
    16 {
    17 //    freopen("post.in","r",stdin);
    18 //    freopen("post.out","w",stdout);
    19     
    20     read(n),read(m);
    21     if(m==n) { puts("0"); return 0; }
    22     for(int i=1; i<=n; ++i) read(x[i]);
    23     for(int i=1; i<=n; ++i)
    24       for(int j=i+1; j<=n; ++j)
    25         minx[i][j]=minx[i][j-1]+x[j]-x[i+j>>1];
    26     memset(f,127/3,sizeof(f));
    27     for(int i=1; i<=n; ++i)
    28         f[i][1]=minx[1][i],f[i][i]=0;
    29     for(int k=2; k<=m; ++k)
    30       for(int i=1; i<=n; ++i)
    31       {
    32           for(int j=i-1; j; --j)
    33           f[i][k]=min(f[i][k],f[j][k-1]+minx[j+1][i]);
    34       }
    35     printf("%d
    ",f[n][m]);
    36     return 0;
    37 }
    38 
    39 int Aptal=Presist();
    40 int main(){;}
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    Hadoop概论
    虚拟机
    Linux的常用命令
    jsoup抓取数据
    分享JQuery动画插件Velocity.js的六种列表加载特效
    html5和css3打造一款创意404页面
    分享一款基于jquery的圆形动画按钮
    一款基于jquery和css3实现的摩天轮式分享按钮
    一款纯css3实现的环形导航菜单
    一款纯css3实现的动画加载导航
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7561558.html
Copyright © 2011-2022 走看看