codevs4203 Mountain Building Primary School

Source: Internet
Author: User

/*Status: F[i][j] The first village has been built with a J School transfer: F[i][j]=min (F[i][j],f[ii][j-1]+s[ii+1][i]) 1<=ii<=i-1*/#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespacestd;intn,m,a[505],h[505],s[505][505],f[505][505],ans;intDfs (intNnintmm) {    inti,j,k; if(f[nn][mm]<0x3f3f3f3f)returnF[NN][MM];//memory (if 10000000 will Tle)    if(NN&LT;=MM)returnf[nn][mm]=0;//every village can be built back 0    if(mm==1)returnf[nn][mm]=s[1][NN];//build only one return maximum (at this point in the midpoint)     for(i=1; i<=nn-1; i++) f[nn][mm]=min (F[nn][mm],dfs (i,mm-1) +s[i+1][nn]); //The transfer represents the establishment of a school at the midpoint of the interval I+1..NN to bring all the villages in this range to school, where S is the sum of the smallest distances.     returnf[nn][mm];}intMain () {inti,j,k,mm; CIN>>n>>m;  for(i=1; i<=n-1; i++) Cin>>A[i];  for(i=1; i<=n;i++) H[i]=h[i-1]+a[i-1];//Prefixes andMemset (F,0x3f,sizeof(f));  for(i=1; i<=n;i++)       for(j=i;j<=n;j++) {mm= (I+J)/2;  for(k=i;k<=j;k++) S[i][j]+=abs (H[mm]-h[k]);//I-->j Build a school minimum distance and (default built in (I+J)/2 place)} ans=Dfs (n,m); cout<<ans; return 0;}

codevs4203 Mountain Building Primary School

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.