3892: [Usaco2014 Dec]Marathon

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 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|.

在二维平面上有N个点,从(x1,y1)到(x2,y2)的代价为|x1-x2|+|y1-y2|。

求从1号点出发,按从1到N的顺序依次到达每个点的最小总代价。

你有K次机会可以跳过某个点,不允许跳过1号点或N号点。

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

Source

Silver

题解:一道神奇的DP,b[i,j]代表从1到i,共计跳跃了j次不难得出递推式

\(b[i,j]=min(b[i-k-1,j-k]+dis(i-k-1,i))\)

,然后\(O\left(N{M}^{2} \right)\)瞎搞万事

 1 /**************************************************************
 2     Problem: 3892
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:808 ms
 7     Memory:4148 kb
 8 ****************************************************************/
 9  
10 var
11    i,j,k,l,m,n:longint;
12    a:array[0..1000,1..2] of longint;
13    b:array[0..1000,0..1000] of longint;
14 function dis(x,y:longint):longint;inline;
15          begin
16               exit(abs(a[x,1]-a[y,1])+abs(a[x,2]-a[y,2]));
17          end;
18 function min(x,y:longint):longint;inline;
19          begin
20               if x<y then min:=x else min:=y;
21          end;
22 begin
23      readln(n,m);
24      for i:=1 to n do readln(a[i,1],a[i,2]);
25      fillchar(b,sizeof(b),-1);
26      b[1,0]:=0;
27      for i:=2 to n do
28          begin
29               for j:=0 to min(i-2,m) do
30                     begin
31                          b[i,j]:=maxlongint;
32                          for k:=0 to j do
33                              if b[i-k-1,j-k]<>-1 then
34                                 begin
35                                      b[i,j]:=min(b[i,j],b[i-k-1,j-k]+dis(i-k-1,i));
36                                 end;
37                     end;
38          end;
39      writeln(b[n,min(n-2,m)]);
40 end.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏HansBug's Lab

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MB Subm...

28770
来自专栏HansBug's Lab

1741: [Usaco2005 nov]Asteroids 穿越小行星群

1741: [Usaco2005 nov]Asteroids 穿越小行星群 Time Limit: 5 Sec  Memory Limit: 64 MB Sub...

28360
来自专栏HansBug's Lab

3433: [Usaco2014 Jan]Recording the Moolympics

3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec  Memory Limit:...

28240
来自专栏HansBug's Lab

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MB...

31470
来自专栏小樱的经验随笔

BZOJ 1597: [Usaco2008 Mar]土地购买【斜率优化+凸包维护】

1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4989 ...

30060
来自专栏HansBug's Lab

2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MB S...

26560
来自专栏HansBug's Lab

2015: [Usaco2010 Feb]Chocolate Giving

2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec  Memory Limit: 162 MB S...

31660
来自专栏HansBug's Lab

1724: [Usaco2006 Nov]Fence Repair 切割木板

1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Su...

30470
来自专栏HansBug's Lab

3301: [USACO2011 Feb] Cow Line

3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MB Submit: ...

334100
来自专栏HansBug's Lab

1682: [Usaco2005 Mar]Out of Hay 干草危机

1682: [Usaco2005 Mar]Out of Hay 干草危机 Time Limit: 5 Sec  Memory Limit: 64 MB Subm...

35440

扫码关注云+社区

领取腾讯云代金券