比赛 20150423 评测结果 AAAAAAAAAAAAAAA
题目名称 马拉松2 最终得分 100
用户昵称 Asm.Def 运行时间 0.229 s
代码语言 C++ 内存使用 2.22 MiB
提交时间 2015-04-23 08:30:51
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
//#define USEFREAD
#ifdef USEFREAD
#define InputLen 5000000
char *ptr=(char *)malloc(InputLen);
#define getc() (*(ptr++))
#else
#define getc() (getchar())
#endif
#define SetFile(x) (freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout))
template<class T>inline void getd(T &x){
	int ch = getc();bool neg = false;
	while(!isdigit(ch) && ch != '-')ch = getc();
	if(ch == '-')ch = getc(), neg = true;
	x = ch - '0';
	while(isdigit(ch = getc()))
		x = x * 10 - '0' + ch;
	if(neg)x = -x;
}
/***********************************************************************/
const int maxn = 502;
typedef long long LL;
int x[maxn], y[maxn], N, K;
LL F[maxn][maxn];

inline int Abs(int x){return x < 0 ? -x : x;}
inline int dist(int i, int j){return Abs(x[i]-x[j]) + Abs(y[i]-y[j]);}

inline void init(){
	getd(N);getd(K);
	for(int i = 0;i < N;++i)getd(x[i]), getd(y[i]);
	memset(F, 0x3f, sizeof(F));
}
int main(){
	#ifdef DEBUG
	freopen("test.txt", "r", stdin);
	#else       
	SetFile(marathonb);
	#endif
	#ifdef USEFREAD
	fread(ptr,1,InputLen,stdin);
	#endif
	
	init();
	int i, j, k;
	F[0][0] = 0, F[1][0] = dist(0, 1);
	for(i = 2;i < N;++i)for(j = 0;j < i && j <= K;++j)
		for(k = 0;j - k >= 0;++k)
			F[i][j] = min(F[i][j], F[i-k-1][j-k] + dist(i-k-1, i));
	LL Ans = 0x3f3f3f3f3f3f3f3fll;
	for(j = 0;j <= K;++j)Ans = min(Ans, F[N-1][j]);
	printf("%lld\n", Ans);
	
#ifdef DEBUG
    printf("\n%.3lf sec \n", (double)clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}