题目大意:有N只怪,每只怪都有相应的A值和B值
现在要求你消灭这N只怪,消灭第i只怪的代价是A[i] + 相邻的B的和
问最小消灭代价
解题思路:在左右两边添加两只A和B都是0的怪
无论消灭顺序如何,A的和是不变的,所以不用考虑A,最后再加就可以了
接着就是区间DP了,用dp[i][j]表示把(i,j)的所有怪都消灭后,独留下第i只怪和第j只怪的最小代价,这就是为什么要在左右添加怪的原因了
接着转移,dp[i][j] = min(dp[i][k] + dp[k][j] + B[i] + B[j])
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;
int A[N], B[N];
int dp[N][N];
int sum, n, cas = 1;
void init() {
scanf("%d", &n);
sum = 0;
B[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
sum += A[i];
}
for (int i = 1; i <= n; i++)
scanf("%d", &B[i]);
n++;
B[n] = 0;
}
void solve() {
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i <= n; i++)
dp[i][i] = dp[i][i + 1] = 0;
for (int len = 2; len <= n; len++) {
for (int i = 0; i + len <= n; i++) {
int j = i + len;
for (int k = i; k <= j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + B[i] + B[j]);
}
}
printf("Case #%d: %d\n", cas++, dp[0][n] + sum);
}
int main() {
int test;
scanf("%d", &test);
while (test--) {
init();
solve();
}
return 0;
}