http://acm.hdu.edu.cn/showproblem.php?pid=5074
Test instructions: give you a matrix score[i][j], and then give you a sequence of numbers, some of which are-1, which means that the number can be replaced by any number of 1~m, and then the maximum value to be asked.
Idea: The two-dimensional dp,dp[i][j] represents the number of I positions for the Max and of J. The maximum sum of this position is pushed by the preceding summation, divided into four cases, (a,-1), (A, B), ( -1,-1), ( -1,b); Dp[i][j]=max (Dp[i][j],dp[i-1][k]+g[k][j]);
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intt,n,m;7 intg[ -][ -];8 inta[ the];9 intdp[ +][ +];Ten One intMain () A { -scanf"%d",&t); - while(t--) the { -scanf"%d%d",&n,&m); - for(intI=1; i<=m; i++) - { + for(intj=1; j<=m; J + +) - { +scanf"%d",&g[i][j]); A } at } - for(intI=1; i<=n; i++) - { -scanf"%d",&a[i]); - } -Memset (DP,0,sizeof(DP)); in for(intI=2; i<=n; i++) - { to if(a[i]>0) + { - if(a[i-1]>0) the { *dp[i][a[i]]=dp[i-1][a[i-1]]+g[a[i-1]][a[i]]; $ }Panax Notoginseng Else - { the for(intj=1; j<=m; J + +) + { ADp[i][a[i]]=max (dp[i][a[i]],dp[i-1][j]+G[j][a[i]]); the } + } - } $ Else $ { - for(intj=1; j<=m; J + +) - { the if(a[i-1]>0) - {WuyiDp[i][j]=max (dp[i][j],dp[i-1][a[i-1]]+g[a[i-1]][j]); the } - Else Wu { - for(intk=1; k<=m; k++) About { $Dp[i][j]=max (dp[i][j],dp[i-1][k]+g[k][j]); - } - } - } A } + } the intans=0; - for(intI=1; i<=m; i++) ans=Max (ans,dp[n][i]); $printf"%d\n", ans); the } the return 0; the}
View Code
HDU 5074 Hatsune Miku