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(NM2)瞎搞万事

 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.
时间: 2024-08-05 16:56:22

3892: [Usaco2014 Dec]Marathon的相关文章

BZOJ 3892 Usaco2014 Dec Marathon DP

题目大意:给出平面上的一些点,要求按顺序遍历,费用是两点之间的曼哈顿距离,可以跳过k次,问最少需要花费多少. 思路:O(n^3)dp就行了. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 510 using namespace std; struct

BZOJ 3892 [Usaco2014 Dec]Marathon 动态规划

题目大意:给定n个点,定义从一个点到另一个点的距离为曼哈顿距离,要求从点1依次走到点n,中途可以跳过k个点不走,求最小距离和 令f[i][j]表示从第一个点走到第i个点中途跳过j次的最小距离和 则有f[i][j]=min{f[i-k-1][j-k]+dis[i-k-1][i]} 时间复杂度O(n^3) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #

[bzoj3892] [Usaco2014 Dec]Marathon

瞎DP一波. f[i][j]:表示已到达或跳过前i个点,总共跳了j个点,并且目前在第i个点的最小总代价. f[i][j]=min{ f[k][j-(i-k-1)] }+dis(k,i),(dis(k,i)表示两点间距离. 时间复杂度O(n^3)本来以为过不了的.. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespac

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 116  Solved: 92[Submit][Status][Discuss] Description Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back

3893: [Usaco2014 Dec]Cow Jog

3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 87[Submit][Status][Discuss] Description The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N

bzoj3891[Usaco2014 Dec]Piggy Back*

bzoj3891[Usaco2014 Dec]Piggy Back 题意: 给定一个N个点M条边的无向图,其中Bessie在1号点,Elsie在2号点,它们的目的地为N号点.Bessie每经过一条边需要消耗B点能量,Elsie每经过一条边需要消耗E点能量.当它们相遇时,它们可以一起行走,此时它们每经过一条边需要消耗P点能量.求它们两个到达N号点时最少消耗多少能量.n,m≤40000. 题解: 先求出以1.2.n为源点的最短路(因为边权为1所以用bfs).答案初始设为1到n的最短路*B+2到n的最

【BZOJ3892】【Usaco2014 Dec】Marathon (Silver and Bronze) 暴力动规

广告 #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43970671"); } 题解 --Silver f[i][j]表示到第i个跳过了j个的最小值 然后暴力从前转移. 它的时间复杂度是1.25亿,但是常数远远远远小于1 --Bronze 跟银组的一样,只不过改改数组大小,然后m直接赋值1

BZOJ 3893 Usaco2014 Dec Cow Jog 模拟

题目大意:给出n头牛他们的初始位置和各自的速度,一头牛追上另一头牛之后这两头牛会变成一头牛,问最后剩下几头牛. 思路:简单模拟一下不难发现,我们只要算出如果正常行驶每头牛的最后到达的地点,从后往前扫一下,有多少个单调不减的序列就是最后有多少头牛. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstring> #include <iostream> #include <algor

BZOJ 3891 Usaco2014 Dec Piggy Back BFS

题目大意:给出一张无向图,有两个人,分别在1和2,他们要到n,一个人走的消耗是c1,c2,两个人一起走是c3,问最少消耗. 思路:题中说是可以一起走,而不是必须一起走,所以之需要看这两个人到所有点的距离,还有每个点到终点的距离,之后枚举从那个点开始一起走,求一下最小值就可以了. CODE: #define _CRT_SECURE_NO_WARNINGS #include <queue> #include <cstdio> #include <cstring> #incl