hdu2829-Lawrence

Lawrence

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


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<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.
 

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

题意:开始有n个点,有n - 1条边连接成一条直线。现在可以删除m条边, 问最后总策略和最小是多少?

总策略等于各个联通分块内各点权值之积,也就是说如果某个范围内的点联通(设为i - j),总策略要加上∑ax * ay(i<=x < y <= j)

题解:首先设dp[i][j]为拆掉i条边后连接前j个点的最小费用。则dp[i][j] = {dp[i - 1][k] + cost[k + 1][j]}

cost[k + 1][j] = ∑ax * ay(k + 1<=x < y <= j),维护两个数组;

第一个数组sum1[i] = a[1] + ..  a[j], 第二个数组sum2[j] = a[1]^2 + ... + a[j]^2

则∑ax * ay(k + 1<=x < y <= j) = ((sum1[i] - sum1[k])^2 - (sum2[i] - sum2[k])) / 2;

再对于dp[i][j]使用四边形优化即可!模板题

AC代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
typedef unsigned long long ll;
 
using namespace std;

const ll maxn = 1111, inf = 0x7fffffffffffffff;
ll sum1[maxn], sum2[maxn], dp[maxn][maxn], s[maxn][maxn];

void init(ll m, ll n){
	for(ll i = 0; i <= m + 1; i++){
		for(ll j = 0; j <= n + 1; j++){
			s[i][j] = 0;
			dp[i][j] = inf;
		}
		dp[i][i] = 0;
		s[i][i] = i;
		s[i][n + 1] = n - 1;
	}
}

ll cost(ll j, ll k){
	return ((sum1[j] - sum1[k]) * (sum1[j] - sum1[k]) - (sum2[j] - sum2[k])) / 2;
}

int main(){
	ll n, m, t;
	while(scanf("%llu %llu", &n, &m) != EOF && (n || m)){
		init(m, n);
		sum1[0] = sum2[0];
		for(ll i = 1; i <= n; i++){
			scanf("%llu", &t);
			sum1[i] = sum1[i - 1] + t;
			sum2[i] = sum2[i - 1] + t * t;
			dp[0][i] = cost(i, 0);
		}
		for(ll i = 1; i <= m; i++){
			for(ll j = n; j >= i; j--){
				for(ll k = s[i - 1][j]; k <= s[i][j + 1]; k++){
					if(dp[i][j] > dp[i - 1][k] + cost(j, k)){
						dp[i][j] = dp[i - 1][k] + cost(j, k);
						s[i][j] = k;
					}
				}
			}
		}
		printf("%llu\n", dp[m][n]);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_37064135/article/details/80450699
hdu