3892: [Usaco2014 Dec]marathon

Source: Internet
Author: User

3892: [Usaco2014 dec]marathon time limit:10 Sec Memory limit:128 MB
submit:169 solved:100
[Submit] [Status] [Discuss] Description

Unhappy with the poor health of he cows, Farmer John enrolls them in an assortment of different physical fitness Activiti Es. His prize cow Bessie was enrolled in a running class, where she was eventually expected to run a marathon through the Downto WN area of the city near Farmer John ' s farm! The marathon course consists of n checkpoints (3 <= N <=) to being visited in sequence, where Checkpoint 1 is the S tarting 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 was, she decides that she'll Skip up-to-K checkpoints (K < N) in order to shorten she 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 had 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 both checkpoints at locations (X1, y1) and (x2, y2) are given by |x1-x2| + |y1-y2|.

There are n points on a two-dimensional plane, and the cost from (X1,y1) to (X2,y2) is |x1-x2|+|y1-y2|. The minimum total cost of each point from 1 to n is obtained from point 1th. You have a k chance to skip a certain point and not allow to skip the 1th or n points.

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

Outputoutput 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 (5) leads to the minimum total distance of 4.

Sample Input5 2
0 0
8 3
1 1
2 2
Sample Output4HINT Source


The puzzle: A magical dp,b[i,j] represents from 1 to I, totaling jumping J times not hard to come up with a recursive b[i,j]=min (B[i-k-1,j-k]+dis (I-k-1,i)), and then O (NM2) to engage in everything

1/**************************************************************2Problem:38923 User:hansbug4 language:pascal5 result:accepted6Time:808Ms7Memory:4148KB8****************************************************************/9  Ten var One I,j,k,l,m,n:longint; AA:Array[0.. +,1..2] ofLongint; -B:Array[0.. +,0.. +] ofLongint; - functionDis (x,y:longint): Longint;inline; the          begin -Exit (ABS (A[X,1]-a[y,1]) +abs (a[x,2]-a[y,2])); -          End; - functionmin (x,y:longint): Longint;inline; +          begin -               ifX<y ThenMin:=xElsemin:=y; +          End; A begin at readln (n,m); -       fori:=1  toN DoREADLN (A[i,1],a[i,2]); -Fillchar (b,sizeof (b),-1); -b[1,0]:=0; -       fori:=2  toN Do -          begin in                forj:=0  toMin (i2, m) Do -                     begin tob[i,j]:=Maxlongint; +                           fork:=0  toJ Do -                              ifb[i-k-1, j-k]<>-1  Then the                                 begin *B[i,j]:=min (b[i,j],b[i-k-1, J-k]+dis (i-k-1, i)); $                                 End;Panax Notoginseng                     End; -          End; theWriteln (B[n,min (n2, M)]); + End.

3892: [Usaco2014 Dec]marathon

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.