原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115
题意:有一排狼,每只狼有一个伤害A,还有一个伤害B。杀死一只狼的时候,会受到这只狼的伤害A和这只狼两边的狼的伤害B的和。如果某位置的狼被杀,那么杀它左边的狼时就会收到来自右边狼的B,因为这两只狼是相邻的了。求杀掉一排狼的最小代价。
解法:设dp[i][j]为消灭i到j只狼的代价,枚举k作为最后一只被杀死的狼,此时会受到a[k]和b[i-1] b[j+1]的伤害 取最小的即可
可列出转移方程:dp[i][j]=min(dp[i][j], dp[i][k-1]+dp[k+1][j]+a[k]+b[i-1]+b[j+1])
dp[i][i]=a[i]+b[i-1]+b[j+1];
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#define INF 99999999
#define eps 0.0001
using namespace std;
int t;
int n;
int dp[210][210];
int a[210];
int b[210];
void DP()
{
for (int l = 0; l < n; l++)
{
for (int i = 1; i + l <= n; i++)
{
int j = i + l;
for (int k = i; k <= j; k++)
dp[i][j] = min(dp[i][j], dp[i][k - 1] + dp[k + 1][j] + a[k] + b[i - 1] + b[j + 1]);
}
}
}
int main()
{
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++)
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
dp[i][j] = INF;
DP();
printf("Case #%d: %d\n", cas, dp[1][n]);
}
return 0;
}