Question Link
Probably there are n male and n female (forgive me for saying so, I am a rough man), and I will give you a matrix of N * n, column J of row I indicates the goodwill of the female (male) to the male (female), and then arranges n pairs of blind dates, ensure that they are all normal (no Lily or similar), and then find out how to arrange to make sense and maximum, and find the maximum value.
I started to try the brute-force method. The time complexity is n! Decisive timeout
#include <iostream>#include <string.h>#include <algorithm>using namespace std;int vis[17];int n, ans, sum;int map[19][19];void dfs(int x){ if (x == n+1) { ans = max(ans, sum); return; } for (int i = 1; i <= n; i++) { if (vis[i] == 0) { sum += map[x][i]; vis[i] = 1; dfs(x+1); vis[i] = 0; sum -= map[x][i]; } }}int main(){ int t; cin >> t; for (int kase = 1; kase <= t; kase++) { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> map[i][j]; sum = 0; ans = 0; memset(vis, 0, sizeof(vis)); dfs(1); cout << "Case " << kase << ": " << ans << endl; } return 0;}
Then, because of a small mental retardation error, WA had five or six times. The Code uses many bitwise operations to compress and store the State conveniently through bitwise operations.
//2013-06-25-22.05#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;const int maxn = 1<<16;int vis[17];int n, ans, sum;int map[19][19];int dp[maxn];int dfs(int x, int d){ if (x == 0) return 0; if (dp[x]) return dp[x]; for (int i = 0; i < n; i++) { if (x&(1<<i)) dp[x] = max(dfs(x^(1<<i), d-1)+map[i+1][d], dp[x]); } return dp[x];}int main(){ int t; cin >> t; for (int kase = 1; kase <= t; kase++) { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> map[i][j]; memset(dp, 0, sizeof(dp)); ans = dfs((1<<n)-1, n); printf("Case %d: %d\n", kase, ans); } return 0;}