## @krydom11月前

**♦♦♦♦♦♦ Description ♦♦♦♦♦♦**

Unhappy with the poor health of his cows, Farmer John enrolls them in an assortment of different physical fitness activities. His prize cow Bessie is enrolled in a running class, where she is eventually expected to run a marathon through the downtown area of the city near Farmer John's farm! The marathon course consists of N checkpoints (3 <= N <= 500) to be visited in sequence, where checkpoint 1 is the starting location and checkpoint N is the finish. Bessie is supposed to visit all of these checkpoints one by one, but being the lazy cow she is, she decides that she will skip up to K checkpoints (K < N) in order to shorten her total journey. She cannot skip checkpoints 1 or N, however, since that would be too noticeable. Please help Bessie find the minimum distance that she has to run if she can skip up to K checkpoints. Since the course is set in a downtown area with a grid of streets, the distance between two checkpoints at locations (x1, y1) and (x2, y2) is given by |x1-x2| + |y1-y2|.

**♦♦♦♦♦♦ Input ♦♦♦♦♦♦**

The first line gives the values of N and K. The next N lines each contain two space-separated integers, x and y, representing a checkpoint (-1000 <= x <= 1000, -1000 <= y <= 1000). The checkpoints are given in the order that they must be visited. Note that the course might cross over itself several times, with several checkpoints occurring at the same physical location. When Bessie skips such a checkpoint, she only skips one instance of the checkpoint -- she does not skip every checkpoint occurring at the same location.

**♦♦♦♦♦♦ Output ♦♦♦♦♦♦**

Output the minimum distance that Bessie can run by skipping up to K checkpoints. In the sample case shown here, skipping the checkpoints at (8, 3) and (10, -5) leads to the minimum total distance of 4.

**♦♦♦♦♦♦ Sample Input ♦♦♦♦♦♦**

5 2

0 0

8 3

1 1

10 -5

2 2

**♦♦♦♦♦♦ Sample Output ♦♦♦♦♦♦**

4

**♦♦♦♦♦♦ Hint ♦♦♦♦♦♦**

**♦♦♦♦♦♦ 题解 ♦♦♦♦♦♦**

设f[i][j]为到第i个点跳过j次的最短距离，然后暴力转移就行了

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<cstdio> #include<cstdlib> #include<algorithm> #define N 510 #define FOR(i,l,r) for(int i=l;i<=r;i++) using namespace std; int n,m,k,inf=1e9,ans,last; int x[N],y[N],f[N][N]; int main() { scanf("%d%d",&n,&m); FOR(i,1,n)scanf("%d%d",x+i,y+i); FOR(i,2,n)FOR(j,0,min(i-1,m)) { f[i][j]=inf; FOR(k,0,j) { int last=i-k-1; f[i][j]=min(f[i][j],f[last][j-k]+abs(x[i]-x[last])+abs(y[i]-y[last])); } } ans=inf; FOR(i,0,min(n-1,m))ans=min(ans,f[n][i]); printf("%d\n",ans); return 0; } |