hdu2829 四邊形最佳化dp

來源:互聯網
上載者: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 British 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 the
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 British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British 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 would 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 should 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初始化為2的60次方才行,這裡錯了好多次,還要int64才行,唉這裡,好坑!看看題目,明顯有dp[i][j]=min(dp[i-1][k]+cos(k,j))這樣,用一個四邊形不等式就可以最佳化了,注意,這裡的cost,也可以用dp求出來,因為有很多的共同因子啊,這樣才能a了去!怎麼求cost呢?明顯cost[i][j]是由cost[i+1][j]+一個第i個因子和(i+1,j)的所有的積的和(記作val[i][j]),那麼怎麼求val[i][j]呢?明顯val[i][j]是由val[i][j-1]+第i數和第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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.