A wave of blind DP.
F[I][J]: Indicates that the first I point has been reached or skipped, a total of J points have been skipped, and the minimum total cost at the point I is currently at.
f[i][j]=min{f[k][j-(i-k-1)]}+dis (k,i), (DIS (k,i) represents the distance between two points.
Time complexity O (n^3) would have thought it was too much.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn=503;7 intX[MAXN],Y[MAXN],DIS[MAXN][MAXN];8 intF[MAXN][MAXN];9 intI,j,k,n,m,kk,ans;Ten One intRA,FH;CharRx; AInlineintRead () { -Rx=getchar (), ra=0, fh=1; - while((rx<'0'|| Rx>'9') &&rx!='-') rx=GetChar (); the if(rx=='-') fh=-1, rx=GetChar (); - while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnra*fh; - } -InlineintAbsintx) {returnx<0?-x:x;} +InlineintMinintAintb) {returnA<b?a:b;} - intMain () { +N=read (), kk=read (); A for(i=1; i<=n;i++){ atX[i]=read (), y[i]=read (); - for(j=1; j<i;j++) Dis[j][i]=abs (X[i]-x[j]) +abs (y[i]-y[j]); - } -Memset (F, -,sizeof(f)); -f[1][0]=0; - for(i=2; i<=n;i++) for(j=0; j<=kk;j++) in for(K=max (i-j-1,1); k<i;k++) F[i][j]=min (f[i][j],f[k][j-(i-k-1)]+dis[k][i]); - for(i=1, ans=f[n][0];i<=k;i++) ans=min (ans,f[n][i]); toprintf"%d\n", ans); +}View Code
[bzoj3892] [Usaco2014 Dec] Marathon