Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5115
Test Instructions: There is a row of wolves, each wolf has a damage a, and a damage B. When you kill a wolf, you will be harmed by the wolf's damage A and the wolf on both sides of the wolf. If a wolf in a position is killed, the wolf on the left will receive B from the right wolf, because the two wolves are adjacent. The minimum cost of killing a row of wolves.
Solution:
set DP[I][J] to eliminate the cost of the I to J Wolf, enumeration K as the last killed Wolf, at this time will be affected by a[k] and B[i-1] b[j+1] To minimize the damage
the transfer equation can be listed: 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];
#include <stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;#defineN 220#defineINF 0XFFFFFFFintMain () {intT, N, A[n], b[n], dp[n][n], t=1; scanf ("%d", &T); while(t--) {memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); scanf ("%d", &N); for(intI=1; i<=n; i++) scanf ("%d", &A[i]); for(intI=1; i<=n; i++) scanf ("%d", &B[i]); for(intI=1; i<=n; i++) { for(intJ=i; j<=n; J + +) Dp[i][j]=INF; ///dp[i][i]=a[i]+b[i-1]+b[i+1]; } for(intL=0; l<=n; l++) { for(intI=1; i+l<=n; i++) { intj=i+l; for(intK=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]); }}} printf ("Case #%d:%d\n", t++, dp[1][n]); } return 0;}
View Code
Dire Wolf---hdu5115 (interval dp)