poj1160 Post Office

Source: Internet
Author: User

The main topic: There are N villages, now to establish m post Office, post Office can only be built in the countryside. Now to make every village to the nearest post office distance of the sum as small as possible, to seek this minimum distance and.

N<300,p<30, the country's location is not more than 10000.

Analysis: This problem is IOI's old problem, so the data size is very small, simple DP can also be too. But as a quadrilateral optimization problem is also very good.

Set F[I][J] Indicates the minimum distance of the first I village and the J Post Office.

F[i][j]=min (F[k][j-1]+w (K+1,i)) where W (k+1,i) represents the sum of the distance from the I+1 village to the first village to the point. Because the post office must be to establish the middle of this paragraph to ensure that the distance and minimum. This is easy to prove, with contradiction.

K as a decision point, if enumerated in interval [1,i], the total event complexity is O (n^3)

But because W (k+1,i) satisfies the interval containment relationship and satisfies the parallelogram relationship (proving to be quite difficult). So f[i][j] Also satisfies the parallelogram principle. S[I][J] represents the best decision point for f[i][j], there is

S[I-1][J]<=S[I][J]<=S[I][J+1]. (1)

But in fact, from the perspective of common sense can be directly introduced in the formula (1).

If the number of villages is constant and the number of post offices increases, then the post-start range should be extended to both sides, at least unchanged; Conversely, if the number of post offices decreases, the starting and ending range should shrink to the middle, at least unchanged.

So s[i][j]<=s[i][j+1].

So, you can apply this equation 1 directly.

If the number of post offices is constant and the number of villages is reduced (the right-most village is excluded), the post office should fine-tune to the left and not move to the right; instead, the post office should fine-tune.

So S[i-1][j]<=s[i][j].

W (I,J) has an O (1) method to find out. We first preprocess the distance from all villages to the left-most village, and seek the prefixes and, as sum1, the distance from the far right rural areas of all the villages, and seek suffixes and sum2.

The median point for "i,j" is K, then W (i,j) =sum1[j]-sum1[k]-(j-k) * (Pos[k]-pos[1]) +sum2[i]-sum2[k]-(k-i) * (Pos[n]-pos[k])

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 3055 using namespacestd;6 intF[MAXN][MAXN],W[MAXN][MAXN],S[MAXN][MAXN],SUM1[MAXN],SUM2[MAXN];7 intpos[maxn],n,p;8 voidPre ()9 {Ten      for(intI=2; i<=n;i++) Onesum1[i]=sum1[i-1]+pos[i]-pos[1]; A      for(inti=n-1; i>=1; i--) -sum2[i]=sum2[i+1]+pos[n]-Pos[i]; -          for(intI=1; i<n;i++) the              for(intj=i+1; j<=n;j++) -             { -             intk= (I+J)/2; -     intres=0; +res+=sum1[j]-sum1[k]-(j-k) * (pos[k]-pos[1]); -res+=sum2[i]-sum2[k]-(k-i) * (pos[n]-pos[k]); +w[i][j]=Res; A         } at } - intMain () - { -scanf"%d%d",&n,&p); -      for(intI=1; i<=n;i++) -scanf"%d",&pos[i]); in pre (); -Memset (F,0x5f,sizeoff); to      for(intI=1; i<=n;i++) +f[i][1]=w[1][i]; -      for(intI=1; i<=n;i++) the          for(intJ=min (i,p); j<=n;j++) *         { $             if(j>=i| | J&GT;P) s[i][j]=i;Panax Notoginseng             } -      for(intI=2; i<=n;i++) the          for(intJ=min (p,i); j>=1; j--) +         { A             if(I==J) {f[i][j]=0, s[i][j]=j-1;Continue;} the              for(intk=s[i-1][j];k<=s[i][j+1];k++) +                 if(f[i][j]>f[k][j-1]+w[k+1][i]) -{f[i][j]=f[k][j-1]+w[k+1][i]; $s[i][j]=K; $                 } -         } -printf"%d\n", F[n][p]); the}
View Code

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.