Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5115
Test instructions
There are n wolves, each wolf has two kinds of properties, a kind of attack value, we did not kill a wolf, then we receive a damage value of
The wolf's attack value is the same as that of the two wolves next to it, and the minimum damage value for all wolves to be killed.
Analysis:
Naked Interval DP
DP[I][J] represents the smallest damage to any wolf in the range i,j.
The state transition equation is
Dp[i][j]=min{dp[i][k]+dp[k+1][j]-b[k]+b[i+1],dp[i][k]+dp[k+1][j]-b[k+1]+b[j+1]} (I<=K<J) We're talking about killing the left first, or the right.
Note that the initial value of a[0],b[0],a[n+1],b[n+1] is 0 when dealing with it.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 210;int dp[ Maxn][maxn];int A[maxn],b[maxn];int Main () {int n,t,cas=1; scanf ("%d", &t); while (t--) {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]); a[0]=a[n+1]=0; b[0]=b[n+1]=0; Memset (Dp,0,sizeof (DP)); 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=i+1;j<=n;j++) dp[i][j]=999999999; for (int len = 2;len<=n;len++) {for (int i=1;i+len-1<=n;i++) {int j=i+len-1; for (int k=i;k<j;k++) {dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+b[i-1]-b[k]); Dp[i][j]=min (dp[i][j],dp[i][k]+dp[k+1][j]+b[j+1]-b[k+1]); }}} printf ("Case #%d:%d\n ", Cas++,dp[1][n]); } return 0;}
HDU5115 Dire Wolf (interval dp)