您的位置:首页 > 其它

bzoj3892【Usaco2014 Dec】Marathon

2015-10-24 18:41 363 查看

3892: [Usaco2014 Dec]Marathon

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 221  Solved: 135

[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。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 505
#define INF 1000000000
using namespace std;
int n,m,ans=INF,a[MAXN][2],f[MAXN][MAXN];
int main()
{
memset(f,0,sizeof(f));
scanf("%d%d",&n,&m);
F(i,1,n) scanf("%d%d",&a[i][0],&a[i][1]);
F(i,2,n) F(j,0,min(i-1,m))
{
f[i][j]=INF;
F(k,0,j)
f[i][j]=min(f[i][j],f[i-k-1][j-k]+abs(a[i-k-1][0]-a[i][0])+abs(a[i-k-1][1]-a[i][1]));
}
F(i,1,min(n-1,m)) ans=min(ans,f
[i]);
printf("%d\n",ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: