标签:
2 5 3 83 86 77 15 93 35 86 92 49 3 3 3 1 2 10 5 36 11 68 67 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42 29 73 21 19 -1 -1 5 -1 4 -1 -1 -1 4 -1
270 625
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 110; 4 int dp[maxn][maxn],score[maxn][maxn],song[maxn]; 5 int main() { 6 int n,m,kase; 7 scanf("%d",&kase); 8 while(kase--) { 9 scanf("%d%d",&n,&m); 10 for(int i = 1; i <= m; ++i) 11 for(int j = 1; j <= m; ++j) 12 scanf("%d",score[i] + j); 13 for(int i = 1; i <= n; ++i) scanf("%d",song + i); 14 memset(dp,-1,sizeof dp); 15 dp[0][0] = 0; 16 for(int i = 1; i <= n; ++i) { 17 for(int j = 0; j <= m; ++j) { 18 if(dp[i-1][j] == -1) continue; 19 if(song[i] == -1) { 20 for(int k = 1; k <= m; ++k) 21 dp[i][k] = max(dp[i][k],dp[i-1][j] + score[j][k]); 22 } else dp[i][song[i]] = max(dp[i][song[i]],dp[i-1][j] + score[j][song[i]]); 23 } 24 } 25 int ret = 0; 26 if(song[n] == -1) { 27 for(int i = 1; i <= m; ++i) ret = max(ret,dp[n][i]); 28 } else ret = dp[n][song[n]]; 29 printf("%d\n",ret); 30 } 31 return 0; 32 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4892879.html