Fzu 1005 Fast Food (DP)

Source: Internet
Author: User

Test instructions: Input n,k, and then enter n number, this n number represents the distance from the starting point, now to select K points, so that the distance and the smallest.

Analysis: We ask to choose K points from n points to minimize the distance, that is, dp[n][k], then dp[n][k] is Dp[n-1][k-1]+sum[n][n],dp[n-2][k-1]+sum[n-1][n],dp[n-3][k-1]+sum [N-2] The minimum value between [n]....dp[1][k-1]+sum[2][n].

where sum[x][y] means to select a point from the X-point to the Y-point at the shortest distance (if the number is singular, select the middle point, even if the middle of the choice of two whichever line)

Memory Search:

#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>using namespacestd;Const intOO =1e9;inta[222];intdp[222][ -];intsum[222][222];intDFS (intNintk) {    if(n<0|| k<0)        returnOo; if(dp[n][k]!=-1)        returnDp[n][k]; if(k>=N) {dp[n][k]=0; return 0; } Dp[n][k]=Oo;  for(intI=1; i<=n; i++) {Dp[n][k]= Min (Dp[n][k], DFS (i-1, K-1)+sum[i][n]);//For the first n points to establish a K-point, can be from the first 1 to n-1 points to establish k-1 points pushed to find the best solution. }    returndp[n][k];}intMain () {intN, M; intKK =1;  while(SCANF ("%d%d", &n, &m), n+m) {a[0] =0;  for(intI=1; i<=n; i++) {scanf ("%d", &A[i]); }         for(intI=1; i<=n; i++) {Sum[i][i]=0;  for(intj=i+1; j<=n; J + +) {Sum[i][j]= sum[i][j-1]+a[j]-a[(I+J)/2];//sum[i][j] means to select a point between I,j, the shortest distance consumed (if the singular, select the middle point, even if the middle of the selection of two)}}} memset (DP,-1,sizeof(DP)); intAns =DFS (n, m); printf ("Chain%d\n", kk++); printf ("Total Distance sum =%d\n\n", ans); }    return 0;}

Dp

#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<cstdlib>#include<stack>using namespacestd;Const intOO =1e9;inta[222];intdp[222][ -];intsum[222][222];intMain () {intN, M; intKK =1;  while(SCANF ("%d%d", &n, &m), n+m) {a[0] =0;  for(intI=1; i<=n; i++) {scanf ("%d", &A[i]); }         for(intI=1; i<=n; i++) {Sum[i][i]=0;  for(intj=i+1; j<=n; J + +) {Sum[i][j]= sum[i][j-1]+a[j]-a[(I+J)/2]; }        }         for(intI=0; i<=n; i++)        {             for(intj=0; j<=m; J + +) {Dp[i][j]=Oo; }} dp[0][0] =0;  for(intI=1; i<=n; i++)        {             for(intk=1; k<=m; k++)            {                 for(intj=0; j<i; J + +)//This layer loops and upper loop which can be outside, because Dp[i][k] only and dp[1][k-1] to dp[i-1][k-1] about {dp[i][k]= Min (Dp[i][k], dp[j][k-1]+sum[j+1][i]);//Enumerate the state from 1 to i-1, update the value of Dp[i][k] }}} printf ("Chain%d\n", kk++); printf ("Total Distance sum =%d\n\n", Dp[n][m]); }    return 0;}

Fzu 1005 Fast Food (DP)

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.