#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) const int N = 105; const int M = 205; int t, n, m, G, d[M + 10], i, x, j, k, l, y, dp[2][6][6][6][6], ans; struct Map { int v, id; } g[N][N]; bool cmp(Map a, Map b) { return a.v > b.v; } int cal(int u, int v) { if (u == 0 || v == 0) return 0; return g[u][v].id; } int main() { scanf("%d", &t); while (t--) { ans = 0; scanf("%d%d%d", &n, &m, &G); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { scanf("%d", &g[i][j].v); g[i][j].id = j; } sort(g[i] + 1, g[i] + 1 + n, cmp); } G += 10; for (i = 1; i <= G; i++) scanf("%d", &d[i]); memset(dp[0], 0, sizeof(dp[0])); for (i = 1; i <= G; i++) { int now = i % 2; int pre = (!now); memset(dp[now], 0, sizeof(dp[now])); if (d[i]) { for (y = 1; y <= 5; y++) { for (j = 0; j <= 5; j++) { if (i > 1 && cal(d[i], y) == cal(d[i - 1], j)) continue; for (k = 0; k <= 5; k++) { if (i > 2 && cal(d[i], y) == cal(d[i - 2], k)) continue; for (l = 0; l <= 5; l++) { if (i > 3 && cal(d[i], y) == cal(d[i - 3], l)) continue; for (x = 0; x <= 5; x++) { if (i > 4 && cal(d[i], y) == cal(d[i - 4], x)) continue; dp[now][y][j][k][l] = max(dp[now][y][j][k][l], dp[pre][j][k][l][x] + g[d[i]][y].v); ans = max(ans, dp[now][y][j][k][l]); } } } } } } else { for (j = 0; j <= 5; j++) { for (k = 0; k <= 5; k++) { for (l = 0; l <= 5; l++) { for (x = 0; x <= 5; x++) { dp[now][0][j][k][l] = max(dp[now][0][j][k][l], dp[pre][j][k][l][x]); ans = max(ans, dp[now][0][j][k][l]); } } } } } } printf("%.2lf\n", ans * 1.0 / 100); } return 0; }
UVA 1379 - Pitcher Rotation(DP + 贪心),布布扣,bubuko.com
UVA 1379 - Pitcher Rotation(DP + 贪心)
原文:http://blog.csdn.net/accelerator_/article/details/25010617