dp[i][j]代表消灭这个区间中的狼消耗的最小代价
转移过程中枚举每一个点作为最后一个杀死点的最小代价
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 207
using namespace std;
int dp[MAX][MAX];
int a[MAX];
int b[MAX];
int main ( )
{
int t,n;
scanf ( "%d" , &t );
int c = 1;
while ( t-- )
{
scanf ( "%d" , &n );
memset ( dp , 0x3f , sizeof ( dp ) );
b[0] = 0 , b[n+1] = 0;
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++ )
dp[i][i] = a[i] + b[i-1] + b[i+1];
for ( int i = 1 ; i < n ; i++ )
for ( int j = 1 ; j <= n-i ; j++ )
for ( int k = j ; k <= j+i ; k++ )
if ( k == j )
dp[j][j+i] =
min (dp[j+1][j+i] + a[k] + b[j-1] + b[j+i+1] , dp[j][j+i]) ;
else if ( k == j+i )
dp[j][j+i] =
min ( dp[j][j+i], dp[j][j+i-1] + a[k] + b[j+i+1]+b[j-1] );
else
dp[j][j+i] =
min ( dp[j][j+i], dp[j][k-1] +b[j-1] + a[k] + b[j+i+1] +dp[k+1][j+i]);
printf ( "Case #%d: " , c++ );
printf ( "%d\n" , dp[1][n] );
}
}