注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

n+e

NewWeb:http://trinkle.is-programmer.com/

 
 
 

日志

 
 

[BZOJ3892][Usaco2014 Dec]Marathon  

2015-03-16 21:21:49|  分类: BZOJ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

3892: [Usaco2014 Dec]Marathon

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 136  Solved: 80
[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

Solution

f[i][k]表示从1号点到i号点老老实实走过了k个点
f[i][k]=min(f[j][k-1]+|x1-x2|+|y1-y2|) (1<=j<i)

Code

/**************************************************************
    Problem: 3892
    User: wjy1998
    Language: C++
    Result: Accepted
    Time:744 ms
    Memory:1824 kb
****************************************************************/
 
#include<cstdio>
#include<cstring>
#define N 510
int n,k,m,f[N][N],i,j,x[N],y[N];
#define abs(x) (x>0?x:-(x))
#define min(a,b) a<b?a:b
int main(){
    scanf("%d%d",&n,&m);m=n-m;
    for(i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
    memset(f,63,sizeof(f));f[1][1]=0;
    for(i=2;i<=n;i++)
    for(j=1;j<i;j++)
    for(k=1;k<=i&&k<=m;k++)
    f[i][k]=min(f[i][k],f[j][k-1]+abs(x[i]-x[j])+abs(y[i]-y[j]));
    printf("%d\n",f[n][m]);
}

  评论这张
 
阅读(83)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018