设为首页 加入收藏

TOP

HDOJ 2829 Lawrence
2015-07-20 17:50:37 来源: 作者: 【 】 浏览:2
Tags:HDOJ 2829 Lawrence


四边形不等式优化DP

Lawrence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2220 Accepted Submission(s): 975


Problem Description T. 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.

Input There 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 Output For 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 1
4 5 1 2
4 2
4 5 1 2
0 0

Sample Output
17
2

Source 2009 Multi-University Training Contest 2 - Host by TJU

#include 
   
    
#include 
    
      #include 
     
       #include 
      
        using namespace std; typedef long long int LL; const int maxn=1100; int n,m; LL a[maxn],sum[maxn]; LL dp[2][maxn],cost[maxn][maxn]; int s[2][maxn]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(int i=1;i<=n;i++) { scanf("%I64d",a+i); sum[i]=sum[i-1]+a[i]; } memset(cost,0,sizeof(cost)); memset(s,0,sizeof(s)); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) cost[i][j]=cost[i][j-1]+(sum[j-1]-sum[i-1])*a[j]; memset(dp,63,sizeof(dp)); for (int i = 1; i <= n; ++i) { dp[0][i]=cost[1][i]; s[0][i]=1; } int now=1,pre=0; for(int j=1;j<=m;j++) { s[now][n+1]=n-1; for(int i=n;i>=j;i--) { for(int k=s[pre][i];k<=s[now][i+1];k++) { int temp=dp[pre][k]+cost[k+1][i]; if(temp
       
        



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 11557 - Code Theft (KMP + H.. 下一篇杭电1171 Big Event in HDU(母函..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·switch520最新的地址 (2025-12-24 19:19:41)
·微信聊天功能使用了 (2025-12-24 19:19:39)
·websocket和普通的so (2025-12-24 19:19:36)
·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)