zoukankan      html  css  js  c++  java
  • POJ1160 Post Office[序列DP]

    Post Office
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 18680   Accepted: 10075

    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

    ---------------------
    在村庄内建邮局,要使村庄到邮局的距离和最小
    ---------------------
    f[i][j]表示前i个村庄中建j个邮局的最小距离,考虑最后一个邮局的位置
    f[i][j]=min{f[k][j-1]+l[k+1][i]}
    l可以递推,建在中点最好,发现l[i][j]=l[i][j-1]+a[j]-a[(i+j)/2]成立
    注意预处理
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int V=305,P=35,INF=1e9;
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int v,p,a[V],l[V][V],f[V][P];
    void dp(){
        for(int i=1;i<=v;i++)
            for(int j=i+1;j<=v;j++)
                l[i][j]=l[i][j-1]+a[j]-a[(i+j)/2];
        for(int i=1;i<=v;i++) for(int j=0;j<=v;j++) f[i][j]=INF,f[i][1]=l[1][i];
        for(int i=1;i<=v;i++)
            for(int j=1;j<=p;j++){
                for(int k=1;k<i;k++)
                    f[i][j]=min(f[i][j],f[k][j-1]+l[k+1][i]);
            }
    }
    int main(int argc, const char * argv[]) {
        v=read();p=read();
        for(int i=1;i<=v;i++)a[i]=read();
        dp();
        printf("%d",f[v][p]);
        return 0;
    }
  • 相关阅读:
    我的ZigBee学习之路
    php form表单post提交获取不到数据,而使用get提交能获取到数据 的解决办法
    Mac phpstorm破解版安装(简单,有效)
    Mac下phpstorm 浏览器出现 502 bad gateway 解决办法
    LayUI之table数据表格获取行、行高亮等相关操作
    钉钉自定义机器人配合SVN钩子事件进行消息的推送实践
    电脑无故失去焦点,罪魁祸首是谁?终极解决办法
    Java实现的电脑已连接WiFi热点的导入导出小工具 wifi备份
    C# DataGridView自定义分页控件
    C# DataGridView控件禁止拷贝数据
  • 原文地址:https://www.cnblogs.com/candy99/p/5828004.html
Copyright © 2011-2022 走看看