标签:des style blog http io color os ar for
分析:记dp[i][j]为第i位取第j位的最大值
#include "stdio.h" #include "string.h" #define MAX 110 int a[MAX]; int dp[MAX][MAX],g[MAX][MAX]; int max(int a,int b) { return a>b?a:b; } int main() { int t; int i,j,k,n,m; int ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1; i<=m; i++) for(j=1; j<=m; j++) scanf("%d",&g[i][j]); for(i=1; i<=n; i++) scanf("%d",&a[i]); memset(dp,0,sizeof(dp)); for(i=2; i<=n; i++) { if(a[i]>0) { if(a[i-1]>0) dp[i][a[i]]=max(dp[i][a[i]],dp[i-1][a[i-1]]+g[a[i-1]][a[i]]); else { for(j=1; j<=m; j++) dp[i][a[i]]=max(dp[i][a[i]],dp[i-1][j]+g[j][a[i]]); } } else { if(a[i-1]>0) { for(j=1; j<=m; j++) dp[i][j]=max(dp[i][j],dp[i-1][a[i-1]]+g[a[i-1]][j]); } else { for(j=1; j<=m; j++) for(k=1; k<=m; k++) dp[i][k]=max(dp[i][k],dp[i-1][j]+g[j][k]); } } } ans=0; for(i=1; i<=m; i++) ans=max(ans,dp[n][i]); printf("%d\n",ans); } return 0; }
标签:des style blog http io color os ar for
原文地址:http://www.cnblogs.com/Blundering-Chen/p/4057711.html