[bzoj3892] [Usaco2014 Dec] Marathon

Source: Internet
Author: User

A wave of blind DP.

F[I][J]: Indicates that the first I point has been reached or skipped, a total of J points have been skipped, and the minimum total cost at the point I is currently at.

f[i][j]=min{f[k][j-(i-k-1)]}+dis (k,i), (DIS (k,i) represents the distance between two points.

Time complexity O (n^3) would have thought it was too much.

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn=503;7 intX[MAXN],Y[MAXN],DIS[MAXN][MAXN];8 intF[MAXN][MAXN];9 intI,j,k,n,m,kk,ans;Ten   One intRA,FH;CharRx; AInlineintRead () { -Rx=getchar (), ra=0, fh=1; -      while((rx<'0'|| Rx>'9') &&rx!='-') rx=GetChar (); the     if(rx=='-') fh=-1, rx=GetChar (); -      while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnra*fh; - } -InlineintAbsintx) {returnx<0?-x:x;} +InlineintMinintAintb) {returnA<b?a:b;} - intMain () { +N=read (), kk=read (); A      for(i=1; i<=n;i++){ atX[i]=read (), y[i]=read (); -          for(j=1; j<i;j++) Dis[j][i]=abs (X[i]-x[j]) +abs (y[i]-y[j]); -     } -Memset (F, -,sizeof(f)); -f[1][0]=0; -      for(i=2; i<=n;i++) for(j=0; j<=kk;j++) in          for(K=max (i-j-1,1); k<i;k++) F[i][j]=min (f[i][j],f[k][j-(i-k-1)]+dis[k][i]); -      for(i=1, ans=f[n][0];i<=k;i++) ans=min (ans,f[n][i]); toprintf"%d\n", ans); +}
View Code

[bzoj3892] [Usaco2014 Dec] Marathon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.