Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5074
The question is to give you m note, N number, and the score is the sum of V [A [I] [A [I + 1, if a [I] is a negative number, you can place a note. Otherwise, you can only place the note he gave.
Q: What is the maximum score?
I first wrote a memory-based search function.
DP (I, j) represents to the I position, put the note labeled as J
So
If a [I + 1] <0, then dp (I, j) = max (dp (I, j), DP (I + 1, K) + V [J] [k]);
Otherwise, dp (I, j) = dp (I + 1, a [I + 1]) + V [J] [A [I + 1];
Then change it to recurrence ~
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 7 int T,n,m; 8 int v[55][55]; 9 int dp[111][55];10 int a[111];11 12 int main(){13 scanf("%d",&T);14 while(T--){15 memset(v,0,sizeof(v));16 memset(dp,0,sizeof(dp));17 memset(a,0,sizeof(a));18 scanf("%d%d",&n,&m);19 for(int i=1;i<=m;i++){20 for(int j=1;j<=m;j++){21 scanf("%d",&v[i][j]);22 }23 }24 for(int i=1;i<=n;i++){25 scanf("%d",&a[i]);26 }27 for(int i=n-1;i>=0;i--){28 for(int j=0;j<=m;j++){29 if( a[i+1]<0 ){30 for(int k=1;k<=m;k++){31 dp[i][j] = max(dp[i][j],dp[i+1][k]+v[j][k]);32 }33 } else {34 dp[i][j] = v[j][a[i+1]]+dp[i+1][a[i+1]];35 }36 }37 }38 printf("%d\n",dp[0][0]);39 }40 }
[HDU 5074] Hatsune Miku (Dynamic Planning)