hdu5115 区间dp(杀死所有狼最小代价)
dp[i][j]=min(dp[i][k-1]+dp[k+1][r]+b[l-1]+b[r+1]+a[k]);
k指这个区间杀的最后一只
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int dp[205][205],a[205],b[205]; 6 int dfs(int l,int r) 7 { 8 if (l>r) dp[l][r]=0; 9 if (dp[l][r]!=-1) return dp[l][r]; 10 int temp=0x3f3f3f3f,k; 11 for (k=l;k<=r;k++) 12 temp=min(temp,dfs(l,k-1)+dfs(k+1,r)+b[l-1]+b[r+1]+a[k]); 13 dp[l][r]=temp; 14 return temp; 15 } 16 int main() 17 { 18 int T,t,n,i; 19 scanf("%d",&T); 20 for (t=1;t<=T;t++) 21 { 22 scanf("%d",&n); 23 memset(dp,-1,sizeof(dp)); 24 memset(a,0,sizeof(a)); 25 memset(b,0,sizeof(b)); 26 for (i=1;i<=n;i++) 27 scanf("%d",&a[i]); 28 for (i=1;i<=n;i++) 29 scanf("%d",&b[i]); 30 printf("Case #%d: %d\n",t,dfs(1,n)); 31 } 32 }