标签:des style io os for div sp on c
Description
Input
Output
Sample Input
3 75 15 21 75 15 28 34 70 5
Sample Output
188 思路:用dp[i][j]表示到i行时下标为j的最大值,首先先预处理出一行时不冲突的状态,那么我们从第一行开始处理,到最后一行就能得到最大值了,然后再转移到与当前一行 不冲突的下一行的状态,滚动数组处理#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1<<21; int status[maxn], n, cnt; int dp[2][maxn]; int ans, g[22][22]; void init() { cnt = 0; for (int i = 0; i < (1<<20); i++) { if (i & (i << 1)) continue; else status[cnt++] = i; } } int main() { init(); while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", &g[i][j]); if (n == 0) { printf("0\n"); continue; } int tot = 1 << n; ans = 0; for (int i = 0; status[i] < tot; i++) { dp[0][i] = 0; for (int j = 0; j < n; j++) if (status[i] & (1<<j)) dp[0][i] += g[0][j]; ans = max(ans, dp[0][i]); } int cur = 0; for (int i = 1; i < n; i++) { cur ^= 1; for (int j = 0; status[j] < tot; j++) { int tmp = 0; dp[cur][j] = 0; for (int k = 0; k < n; k++) if (status[j] & (1 << k)) tmp += g[i][k]; for (int k = 0; status[k] < tot; k++) if ((status[j] & status[k]) == 0) dp[cur][j] = max(dp[cur][j], dp[cur^1][k]+tmp); ans = max(ans, dp[cur][j]); } } printf("%d\n", ans); } return 0; }
标签:des style io os for div sp on c
原文地址:http://blog.csdn.net/u011345136/article/details/39436005