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

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

 
题目大意:
 
公司组织婚礼, 有 n 个男生和 n 个女生匹配, 不同的人匹配会有不同的匹配值, 求最大的匹配值
 
dp[i][j] i代表前i个人,j 包含了将要匹配的所有状态(0为未匹配,1为已经匹配)
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; #define N 1100 #define met(a,b) (memset(a,b,sizeof(a)))
typedef long long LL; int a[][], dp[][(<<)]; int main()
{
int T, iCase=; scanf("%d", &T); while(T--)
{
int n, i, j, k, K; scanf("%d", &n); for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d", &a[i][j]); K = (<<n)-;
for(i=; i<=n; i++)
{
for(j=; j<=K; j++)
{
dp[i][j] = ;
int cnt = ;
for(k=; k<n; k++)///cnt算出j这个状态有多少个人匹配
if(j&(<<k)) cnt++;
///如果有i个人匹配, 便可进行下面的状态转化
if(cnt!=i) continue;
for(k=; k<=n; k++)
{
if( (j&(<<(k-))) )
dp[i][j] = max(dp[i][j], dp[i-][j-(<<(k-))]+a[i][k]);
}
}
} printf("Case %d: %d\n", iCase++, dp[n][K]);
}
return ;
}

(状压) Marriage Ceremonies (lightOJ 1011)的更多相关文章

  1. Marriage Ceremonies LightOJ - 1011

    Marriage Ceremonies LightOJ - 1011 常规状压dp.popcount(S)表示S集合中元素数量.ans[S]表示S中的女性与前popcount(S)个男性结婚的最大收益 ...

  2. Light OJ 1011 - Marriage Ceremonies(状压DP)

    题目大意: 有N个男人,和N个女人要互相匹配,每个男人和每个女人有个匹配值. 并且匹配只能是1对1的. 问所有人都匹配完成,最大的匹配值是多少?   状压DP,暴力枚举就OK了, 这个题目略坑,因为他 ...

  3. Lightoj 1011 - Marriage Ceremonies

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

  4. lightoj 1158 - Anagram Division(记忆化搜索+状压)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1158 题解:这题看起来就像是记忆搜索,由于s很少最多就10位所以可以考虑用状压 ...

  5. lightoj 1086 - Jogging Trails(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1086 题解:题目就是求欧拉回路然后怎么判断有欧拉回路只要所有点的度数为偶数.那 ...

  6. lightoj 1061 - N Queen Again(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1061 题解:显然能满足情况的8皇后的摆法不多,于是便可以用题目给出的状态来匹配 ...

  7. lightoj 1119 - Pimp My Ride(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1119 题解:状压dp存一下车有没有被搞过的状态就行. #include < ...

  8. lightoj 1037 - Agent 47(状压dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1037 #include <iostream> #include & ...

  9. Lightoj1011 - Marriage Ceremonies

    1011 - Marriage Ceremonies   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

随机推荐

  1. 性能测试框架Locust初学笔记

    Locust初探 Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本. locust提供web ui界面,能够方便用户实时监控脚本运行状态. ...

  2. [翻译]用神经网络做回归(Using Neural Networks With Regression)

    本文英文原文出自这里, 这个博客里面的内容是Java开源, 分布式深度学习项目deeplearning4j的介绍学习文档. 简介: 一般来说, 神经网络常被用来做无监督学习, 分类, 以及回归. 也就 ...

  3. lintcode-【简单题】合并区间

    题目: 给出若干闭合区间,合并所有重叠的部分. 样例: 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 1 ...

  4. 为什么C++中空类和空结构体大小为1?(转载)

    原文链接:http://www.spongeliu.com/260.html 对于结构体和空类大小是1这个问题,首先这是一个C++问题,在C语言下空结构体大小为0(当然这是编译器相关的).这里的空类和 ...

  5. 第三章 数组与字符串 UVa1588 Kickdown

    题目要求简述:给定长度分别为n1,n2(n1,n2<=100)且每列的高度只为1或者2的长条.需要将他们放入一个高度为3的容器,问能够容纳它们的最短容器长度. 分析: 对于这样的题目显而易见有两 ...

  6. SQL笔记-第三章,数据的增删改

    1.数据的插入 简单的INSERT语句 INSERT INTO T_Person(FName,FAge,FRemark) VALUES(‘Tom’,18,’USA’) 简化的INSERT语句(只对部分 ...

  7. 设置PATH变量

    一不小心把PATH变量清空了,所有的命令都执行不了了,提示“xxx: command not found”,解决办法:在命令行输入export PATH=/usr/local/sbin:/usr/lo ...

  8. js onkeypress与onkeydown 事件区别详细说明

    keypress只适用于有字符输入的按键 而keyup/keydown包含了Ctrl, Shift之类的情况 Firefox在处理onKeyDown/onKeyPress事件时存在漏洞,恶意网页可能利 ...

  9. json 特殊字符 javascript 特殊字符处理(转载)

    特殊字符以前都是禁止页面输入,这样就简单不容易出错,但最近需求要求能输入特殊字符整理出java返回json时特殊字符的转义(不转义会破坏json数据格式导致页面读取数据出错) Java代码 publi ...

  10. python获取DBLP数据集

    #!/usr/bin/python # -*- coding: UTF-8 -*- import xml.sax import io, sys paper_tags = ('article', 'in ...