1011 - Marriage Ceremonies(状压dp)

本文通过状压DP的方法解决了一个婚姻匹配问题,旨在最大化匹配后的总优先级指数。问题来源于LightOJ 1011 Marriage Ceremonies,需要在给定男女双方优先级矩阵的情况下,安排N场婚礼使得总优先级最高。

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

1011 - Marriage Ceremonies
Time Limit: 2 second(s)Memory Limit: 32 MB

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 jthinteger 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


第一道状压dp,看着A巨的博客写的,哭~~

/**第一题状压**/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int SIZE=1e5+10;
const int maxn=500000;
int cmap[20][20],n,dp[17][70000];//dp[i][j]表示前i行状态为j的max
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&cmap[i][j]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            for(int j=0;j<(1<<n);j++){//枚举了所有的情况,由前i-1行推出前i行的所有情况,取最大值
                int cnt=0;
                for(int k=0;k<n;k++)
                    if(j&(1<<k))cnt++;
                if(cnt!=i-1)continue;
                for(int k=0;k<n;k++){
                    if(j&(1<<k))continue;
                    dp[i][j|(1<<k)]=max(dp[i][j|(1<<k)],dp[i-1][j]+cmap[i][k+1]);//设k为2,1<<k为100,1的位置是j中的第三个,也就是未匹配的位置;
                }
            }
        }
        printf("Case %d: %d\n",cas,dp[n][(1<<n)-1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值