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)