3892: [Usaco2014 Dec]Marathon
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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|.
Input
Output
Sample Input
0 0
8 3
1 1
10 -5
2 2
Sample Output
HINT
Source
题解:
dp,f[i][j]表示走到i,用了j次跳转。
转移方程:f[i][j]=min(f[i][j],f[k][j-(i-k-1)]+abs(x[i]-x[k])+abs(y[i]-y[k]);
<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
#define inf 1e15
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll n,m,f[505][505],x[505],y[505];
int main()
{
n=read();m=read();
for(int i=1;i<=n;i++)x[i]=read(),y[i]=read();
memset(f,127/3,sizeof(f));
f[1][0]=0;
for(int i=2;i<=n;i++)
for(int j=0;j<=i-2,j<=m;j++)
for(int k=i-1;i-k-1<=j,k;k--)
f[i][j]=min(f[i][j],f[k][j-(i-k-1)]+abs(x[i]-x[k])+abs(y[i]-y[k]));
ll ans=inf;
for(int i=0;i<=m;i++)ans=min(ans,f[n][m]);
cout<<ans<<endl;
}
</span>