题解 | #Most Powerful#
Most Powerful
https://ac.nowcoder.com/acm/problem/15832
题解思路
用状态压缩dp 0表示未消失 1表示已经消失
题目代码
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 12, M = 1 << 10; int n; int a[N][N], f[M]; int main() { memset(a, 0, sizeof a); while (cin >> n && n) { for (int i = 0; i < n; i ++ ) for (int j = 0; j < n; j ++ ) cin >> a[i][j]; memset(f, 0, sizeof f); for (int i = 0; i < 1 << n; i ++ ) { for (int j = 0; j < n; j ++ ) { if (i >> j & 1) continue; for (int k = 0; k < n; k ++ ) { if (i >> k & 1) continue; if (j == k) continue; f[i | 1 << j] = max(f[i | 1 << j], f[i] + a[k][j]); } } } int ans=0; for (int i = 0; i < 1 << n; i ++ ) ans = max(ans, f[i]); printf("%d\n",ans); } return 0; }