poj1160 Post Office

Source: Internet
Author: User

Description

There is a straight highway with villages alongside the highway. The highway is represented as a integer axis, and the position of each village are identified with a single integer Coordi Nate. There is no villages in the same position. The distance between and positions is the absolute value of the difference of the their integer coordinates.

Post Offices is built in some and not necessarily all of the villages. A Village and the post office in it has the same position. For building the post offices, their positions should is chosen so, the total sum of all distances between each villag E and its nearest post office is minimum.

You is to write a program which, given the positions of the villages and the number of post offices, computes the least P Ossible sum of all distances between, village and its nearest post office.

Input

Your program was to read from standard input. The first line contains integers:the first was the number of villages V, 1 <= v <=, and the second is the Nu Mber of Post offices p, 1 <= p <=, p <= v. The second line contains V integers in increasing order. These V integers is the positions of the villages. For each position x it holds 1 <= x <= 10000.

Output

The first line contains one integer S, which are 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

This question is the interval DP classics, looked the other people's code, wrote out oneself. It is possible to set up two arrays of sum[i][j] and dp[i][j],sum[i][j] to denote the establishment of a post office between the subsections I and J villages and the cost of these villages and the minimum cost of the Post office calculation, Dp[i][j] Represents the first I village of the middle J Post office and the cost of this I village is calculated with the smallest costs associated with these post offices.

So the minimum cost of a J Post office in the first I village can be i-1 by the minimum cost of a post office in the former K village plus the K Post office to the first post office to build a post office minimum cost to transfer over, namely Dp[i][j]=min (dp[i][j],dp[k][j-1]+sum[k+1][ I]);

The initial conditions here are dp[i][1]=sum[1][i].

When the post office number is 1 o'clock, we build the post office in the middle of the line, when the post office has more than one, there is sum[i][j]=sum[i][j-1]+pos[j]-pos[(I+J)/2].

#include <stdio.h> #include <string.h> #define MAXN 350#define inf 88888888int min (int a,int b) {return a<b? A:B;} int Pos[maxn],sum[maxn][maxn],dp[maxn][35];int Main () {int n,m,i,j,k;while (scanf ("%d%d", &n,&m)!=eof) { Memset (Dp,inf,sizeof (DP)), memset (sum,0,sizeof (sum)), for (i=1;i<=n;i++) {scanf ("%d", &pos[i]);} if (n==m) {printf ("0\n"); continue;} for (i=1;i<=n-1;i++) {for (j=i+1;j<=n;j++) {sum[i][j]=sum[i][j-1]+pos[j]-pos[(I+J)/2];}} for (i=1;i<=n;i++) {dp[i][1]=sum[1][i];} for (j=2;j<=m;j++) {to (i=j+1;i<=n;i++) {for (k=1;k<=i;k++) {dp[i][j]=min (Dp[i][j],dp[k][j-1]+sum[k+1][i]) ;}}} printf ("%d\n", Dp[n][m]);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

poj1160 Post Office

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.