hdu Lawrence
http://acm.hdu.edu.cn/showproblem.php?pid=2829
题意:有n个点,每个点有路与两侧点相连,要去掉一些路,使真个系统值最小
计算方法:
4----5-----1-----2
Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.
4----5 1-----2
The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22.
4 5-----1------2
The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1100;
__int64 m, n, ans1[maxn], ans2[maxn], mit[maxn], dp[maxn][maxn], w[maxn][maxn], lit[maxn][maxn];
void dynamic(){
int i, j, k, aa, bb, tmp;
for( i= 1; i<= n; i++){
for( j= i+1; j<= n; j++){
w[i][j] = ((ans1[j] - ans1[i-1])*(ans1[j] - ans1[i-1]) - (ans2[j] - ans2[i-1]))/2;
}
}
for( i= 0; i<= n; i++){
dp[0][i] = w[1][i];
lit[0][i] = 0;
}
for( i= 1; i<= m ; i++){
for( j= n; j>= 1; j--){
lit[i][n+1] = n;
aa = lit[i-1][j];
bb = lit[i][j+1];
for( k= aa; k<= bb; k++){
tmp = dp[i-1][k] + w[k+1][j];
if( tmp < dp[i][j]){
dp[i][j] = tmp;
lit[i][j] = k;
}
}
}
}
printf("%I64d\n", dp[m][n]);
}
int main(){
// freopen("1.txt", "r", stdin);
int i, j, k;
while( scanf("%I64d%I64d", &n, &m) && !( m== 0 && n== 0)){
mit[0] = ans1[0] = ans2[0] = 0;
for( i= 1; i<= n; i++){
scanf("%d", &mit[i]);
ans1[i] = ans1[i-1] + mit[i];
ans2[i] = ans2[i-1] + mit[i]*mit[i];
}
memset( dp, 0x7f, sizeof( dp));
dynamic();
}
return 0;
}