Hdu2829 quadrilateral optimized DP

Source: Internet
Author: User
Lawrence

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1701 accepted submission (s): 737

Problem descriptiont. e. lawrence was a controversial figure during World War I. he was a briish officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. his primary targets were
Railroads. A highly fictionalized version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia ".

You are to write a program to help Lawrence figure out how to best use his limited resources. you have some information from briish intelligence. first, the rail line is completely linear --- there are no branches, no spurs. next, briish intelligence has assigned
A strategic importance to each depot --- an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. the strategic value of the entire railroad is calculated by adding up the products of the strategic values
For every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:

Its strategic value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.

Now, suppose that Lawrence only has enough resources for one attack. he cannot attack the depots themselves --- they are too well defended. he must attack the rail line between depots, in the middle of the desert. consider what wowould happen if Lawrence attacked
This rail line right in the middle:



The strategic value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:


The strategic value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest strategic value that he can achieve for that railroad.

Inputthere will be several data sets. each data set will begin with a line with two integers, N and M. n is the number of depots on the railroad (1 ≤ n ≤ 1000), and m is the number of attacks Lawrence has resources for (0 ≤ m <n ). on the next
Line will be n integers, each from 1 to 100, indicating the strategic value of each depot in order. end of input will be marked by a line with n = 0 and m = 0, which shoshould not be processed.

Outputfor each data set, output a single integer, indicating the smallest strategic value for the railroad that Lawrence can achieve with his attacks. output each integer in its own line.

Sample Input

4 14 5 1 24 24 5 1 20 0
 

Sample output

172 is initialized to the power of 60 of 2. It has been wrong many times, and int64 is required! Look at the question, obviously DP [I] [J] = min (DP [I-1] [k] + cos (K, j) This way, you can use a quadrilateral inequality to optimize it. Note that the cost here can also be obtained using DP, because there are many common factors so that a can be used! How can we find cost? Obviously, cost [I] [J] is composed of cost [I + 1] [J] + the first I factor and (I + 1, J) the sum of all product values (recorded as Val [I] [J]). How can we find Val [I] [J? Obviously Val [I] [J] is the product of Val [I] [J-1] + number I and number J!
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define MAXN 1005#define inf  (__int64)1<<60__int64 prime[MAXN],dp[MAXN][MAXN],kk[MAXN][MAXN],cost[MAXN][MAXN],val[MAXN][MAXN];int main(){    int n,m,i,j,k;    while(scanf("%d%d",&n,&m)!=EOF)    {       if(n==0&&m==0)       break;       for(i=1;i<=n;i++)       {           scanf("%I64d",&prime[i]);       }       memset(cost,0,sizeof(cost));       memset(val,0,sizeof(val));       for(i=1;i<n;i++)          for(j=i+1;j<=n;j++)          {              val[i][j]=val[i][j-1]+prime[i]*prime[j];          }       for( i=n-1;i>=1;--i)        {            for( j=i+1;j<=n;++j)            {                cost[i][j]=cost[i+1][j]+val[i][j];                //printf("cost %d %d\n",cost[i][j],ff(i,j));            }        }       for(i=0;i<=m;i++)            for(j=0;j<=n;j++)            {                dp[i][j]=inf;            }       for(i=1;i<n;i++)       {           dp[0][i]=cost[1][i];           kk[0][i]=1;       }       for(i=1;i<=m;i++)       {           kk[i][n+1]=n-1;           for(j=n;j>i;j--)           {               for(k=kk[i-1][j];k<=kk[i][j+1];k++)               {                   int temp=dp[i-1][k]+cost[k+1][j];                   if(temp<dp[i][j])                   {                       dp[i][j]=temp;                       kk[i][j]=k;                   }               }           }       }       printf("%I64d\n",dp[m][n]);    }    return 0;}

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.