HDU 1227 Fast Food (DP)

Source: Internet
Author: User

Test instructions

There are n restaurants on the x-axis. Location is d[1] ... D[n].

There is a K food storage point. Each food storage point must be in the same place as a restaurant.

Calculates the minimum value of sum (the location of the nearest storage point from the di-).

1 <= n <=, 1 <= k <=, K <= N

Ideas:

Position of the K storage point if determined, the position of the former K-1 storage point is floating. There are a lot of repeating sub-structures. The structure of the DP is obvious.

DP[I][J]: The minimum value given to the position of the I storage point at the location of the J-restaurant.

Code:

intn,k;intpos[205];intdp[ *][205];intCalcintPreintNow ) {    intans=0; Rep (I,pre,now) {ans+=min (pos[i]-pos[pre],pos[now]-Pos[i]); }    returnans;}intCALC2 (intLast ) {    intans=0; Rep (I,last,n) {ans+ = (pos[i]-Pos[last]); }    returnans;}intMain () {intt=0;  while(SCANF ("%d%d", &n,&k)!=eof,n| |k) {Rep (i,1, N) scanf ("%d",&Pos[i]); Sort (POS+1, pos+1+N);        MEM (Dp,inf); dp[1][1]=0; Rep (now,1, n-k+1) {dp[1][now]=0; Rep (J,1, now-1) {dp[1][now]+= (pos[now]-Pos[j]); }} rep (I,2, k) {//The first I depotRep (now,i,n-k+i) {//number of the first depot placedRep (pre,i-1, now-1){//number of depot placed in section i-1Dp[i][now]=min (dp[i][now],dp[i-1][pre]+Calc (pre,now)); }            }        }        intans=inf; Rep (Last,k,n) {ans=min (ans,dp[k][last]+CALC2 (last)); } printf ("Chain%d\n",++T); printf ("Total Distance sum =%d\n\n", ans); }    return 0;}

HDU 1227 Fast Food (DP)

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.