标签:des style blog color io os ar java for
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 162 Accepted Submission(s): 129
1 //============================================================================ 2 // Name : 1.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include <algorithm> 11 #include <stdio.h> 12 #include <string.h> 13 #include <math.h> 14 #include <stdlib.h> 15 #include <queue> 16 using namespace std; 17 18 int score[110][110],f[110]; 19 int dp[110][110]; 20 int t,n,k; 21 22 int main() { 23 //freopen("in.txt","r",stdin); 24 25 scanf("%d",&t); 26 while(t--){ 27 scanf("%d %d",&n,&k); 28 for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) scanf("%d",&score[i][j]); 29 for(int i=1;i<=n;i++) scanf("%d",&f[i]); 30 31 memset(dp,-1,sizeof(dp)); 32 if(f[1]==-1) 33 for(int i=1;i<=k;i++) dp[1][i]=0; 34 else dp[1][f[1]]=0; 35 for(int i=2;i<=n;i++){ 36 if(f[i]==-1){ 37 for(int z=1;z<=k;z++){ 38 int maxc = -1; 39 for(int j=1;j<=k;j++) 40 if(dp[i-1][j] + score[j][z] > maxc && dp[i-1][j]!=-1) maxc = dp[i-1][j] + score[j][z]; 41 dp[i][z] = maxc; 42 } 43 } 44 else{ 45 //为特定数字的时候,比较特殊! 46 int maxc = -1; 47 int z = f[i]; 48 if(f[i-1]!=-1) { 49 maxc = 0; 50 for(int l=1;l<=k;l++){ 51 if(dp[i-1][l]==-1) maxc = maxc + 1; 52 maxc += dp[i-1][l]; 53 } 54 maxc += score[f[i-1]][z]; 55 } 56 else 57 for(int j=1;j<=k;j++) 58 if(dp[i-1][j] +score[j][z] > maxc && dp[i-1][j]!=-1) maxc = dp[i-1][j] +score[j][z]; 59 dp[i][z] = maxc; 60 for(int zz=1;zz<=k;zz++) 61 if(zz!=z) dp[i][zz] = -1; 62 } 63 } 64 // for(int i=1;i<=n;i++){ 65 // for(int j=1;j<=k;j++) 66 // printf("%d ",dp[i][j]); 67 // printf("\n"); 68 // } 69 int maxc = -1; 70 for(int i=1;i<=k;i++) 71 if(dp[n][i] >maxc) maxc = dp[n][i]; 72 printf("%d\n",maxc); 73 } 74 75 //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! 76 return 0; 77 }
标签:des style blog color io os ar java for
原文地址:http://www.cnblogs.com/songacm/p/4045906.html