light oj 1011 - Marriage Ceremonies (状压 dp)

http://lightoj.com/volume_showproblem.php?problem=1011

1011 - Marriage Ceremonies

You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job is quite hard for you.

The job gets more difficult when people come here and give their bio-data with their preference about opposite gender. Some give priorities to family background, some give priorities to education, etc.

Now your company is in a danger and you want to save your company from this financial crisis by arranging as much marriages as possible. So, you collect N bio-data of men and N bio-data of women. After analyzing quite a lot you calculated the priority index of each pair of men and women.

Finally you want to arrange N marriage ceremonies, such that the total priority index is maximized. Remember that each man should be paired with a woman and only monogamous families should be formed.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains an integer N (1 ≤ n ≤ 16), denoting the number of men or women. Each of the next N lines will contain N integers each. The jth integer in the ith line denotes the priority index between the ith man and jth woman. All the integers will be positive and not greater than 10000.

Output

For each case, print the case number and the maximum possible priority index after all the marriages have been arranged.

Sample Input

Output for Sample Input

2

2

1 5

2 1

3

1 2 3

6 5 4

8 1 2

Case 1: 7

Case 2: 16

 典型的二分匹配,val[i][j]表示第i个男人和第j个女人匹配的值,求最大匹配值

不过最近在练习状压,就用状压写了,由于不是很理解状压,所以看学长博客上说这道题卡常数,我没看出来怎么卡常数的

下边是状压dp代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;
typedef long long LL;

#define N 110
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-5
#define met(a, b) memset (a, b, sizeof (a))

int val[N][N], dp[20][100000];

int main ()
{
    int t, n, nCase = 1;
    scanf ("%d", &t);

    while (t--)
    {
        met (val, 0);
        met (dp, 0);

        scanf ("%d", &n);
        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++)
                scanf ("%d", &val[i][j]);

        int Lim = (1<<n) - 1;

        for (int i=1; i<=n; i++)
        {
            for (int j=0; j<=Lim; j++)
            {
                int cnt = 0;
                dp[i][j] = 0;

                for (int k=0; k<n; k++)
                    if (j & (1<<k)) cnt++;

                if (cnt != i) continue;

                for (int k=1; k<=n; k++)
                    if (j & (1<<(k-1)))
                        dp[i][j] = max (dp[i][j], dp[i-1][j-(1<<(k-1))] + val[i][k]);
            }
        }
        printf ("Case %d: %d\n", nCase++, dp[n][Lim]);
    }
    return 0;
}


你可能感兴趣的:(light oj 1011 - Marriage Ceremonies (状压 dp))