标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 931 Accepted Submission(s): 651
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
/** 题意:m种点,一个长度为n的串,若串中的数为-1 则为待定,否则为这点 求这个串的最大价值, 做法:dp 分为四种情况,如果当前点是未知 前一个点已知 前一个点未知 如果当前点已知 前一个点已知 前一个点未知 情况很少 很容易枚举 ============================================================ dp是硬伤 ============================================================ **/ #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #include <stdio.h> #include <queue> #define maxn 110 using namespace std; int score[maxn][maxn]; int note[maxn]; int dp[maxn][maxn]; int main() { int T; scanf("%d", &T); while(T--) { int n, m; scanf("%d %d", &n, &m); for(int i = 1; i <= m; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &score[i][j]); } } for(int i = 1; i <= n; i++) { scanf("%d", ¬e[i]); } memset(dp, 0, sizeof(dp)); for(int i = 2; i <= n; i++) { if(note[i] > 0) { if(note[i - 1] > 0) { dp[i][note[i]] = dp[i - 1][note[i - 1]] + score[note[i - 1]][note[i]]; } else { for(int j = 1; j <= m; j++) { dp[i][note[i]] = max(dp[i][note[i]], dp[i - 1][j] + score[j][note[i]]); } } } else { if(note[i - 1] > 0) { for(int j = 1; j <= m; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][note[i - 1]] + score[note[i - 1]][j]); } } else { for(int j = 1; j <= m; j++) { for(int k = 1; k <= m; k++) { dp[i][j] = max(dp[i][j], dp[i - 1][k] + score[k][j]); } } } } } int sum = -1; if(note[n] > 0) { sum = dp[n][note[n]]; } else { for(int i = 1; i <= m; i++) { sum = max(sum, dp[n][i]); } } printf("%d\n", sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4741154.html