1011 - Marriage Ceremonies[状态压缩dp]

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

 


PROBLEM SETTER: JANE ALAM JAN


这是我第一次写状压dp,以前都觉得状压dp嘛,也就那么回事,但是今天真正的写到状压dp才知道自己简直烂透了。。

题目大意是。。从n×n的矩阵中每行每列都不冲突的选择出n个数来,问最大的和是多少

状态:dp[i][mask]是指从前i行里选出列的状态为mask的最大和
状态转移为:dp[i][mask|(1<<(j-1))] = max(dp[i][mask|(1<<(j-1))] , dp[i-1][mask]+a[i][j])  其中 dp[i-1][mask]>0并且(mask>>(j-1))&1 == 0
逗比的是。。不知道怎么由上一个mask转移到当前mask自己竟然天真的用队列来写,wa了N多记啊。。也不知道是怎么wa的。。
其实只需要判断当前的mask是否是合法的就行啦。。。。果然还是我太年轻。。。

见代码:
#include <bits/stdc++.h>
using namespace std;

int T,n,a[20][20];
int dp[20][1<<17];

int main(){
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        // 先初始给第一行的每个状态赋上初值
        for(int i=1;i<=n;i++){
            dp[1][1<<(i-1)] = a[1][i];
        }
        for(int i=2;i<=n;i++){
            for(int mask = 0;mask<1<<n;mask++)if(dp[i-1][mask]>0){  // 如果上一个状态是合法的
                for(int j=1;j<=n;j++){
                    // 如果当前的列还未选中
                    if(mask>>(j-1)&1) continue;
                    dp[i][mask|(1<<(j-1))] = max( dp[i][mask|(1<<(j-1))] ,dp[i-1][mask]+a[i][j]);
                }
            }
        }
        printf("Case %d: %d\n",t,dp[n][(1<<n)-1]);
    }
    return 0;
}

要多多总结才是,自己的不足还很多。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值