Poj 1160 Post Office (Dynamic Planning)

Source: Internet
Author: User

Post Office
Time limit:1000 ms   Memory limit:10000 K
Total submissions:15412   Accepted:8351

Description

There is a straight highway with ages 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 ages 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 ages. A village and the post office in it have the same position. for building the post offices, their positions shocould 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 versions 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 ages 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 ages. 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 51 2 3 6 7 9 11 22 44 50

Sample output

9

Source

IOI 2000

Question:

There are n villages and m post offices. The coordinates of each village tell you that m post offices are now set up in these N villages. What is your minimum cost? The cost is the distance from each village to the nearest post office.


Solution:

DP [I] [J] records the minimum cost of J villages in I post office, cost [k + 1] [J], the minimum cost of setting up a post office in village k + 1 to Village J is recorded.

So: DP [I] [J] = min {DP [I] [k] + cost [k + 1] [J]}

Finally, output DP [m] [N.

But what is the minimum cost for setting up a post office in village k + 1 to Village J? Answer: set the median value to the village in the middle.


Solution code:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int maxn=310;const int maxm=40;int cost[maxn][maxn],dp[maxm][maxn],a[maxn];int n,m,s[maxn][maxn];void input(){    for(int i=1;i<=n;i++) scanf("%d",&a[i]);    memset(dp,-1,sizeof(dp));    for(int i=1;i<=n;i++){        for(int j=i+1;j<=n;j++){            cost[i][j]=0;            int mid=(i+j)/2;            for(int k=i;k<=j;k++){                cost[i][j]+=abs(a[k]-a[mid]);            }        }    }}int DP(int c,int r){    if(dp[c][r]!=-1) return dp[c][r];    if(c==1) return cost[1][r];    int ans=(1<<30);    for(int i=1;i<r;i++){        int tmp=DP(c-1,i)+cost[i+1][r];        if(tmp<ans) ans=tmp;    }    return dp[c][r]=ans;}void solve(){    cout<<DP(m,n)<<endl;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        input();        solve();    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.