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. noip2007解题报告

    T1.统计数字 给出n个数,统计每个数字出现的个数. n小,快排解决. T2.字符串的展开 给出一个字符串,其中形如 d-h,4-9之类的就展开,(前面比后面小的保留,相等也是),三个参数,P1表示大 ...

  2. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  3. 纸上谈兵:栈(stack)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 栈(stack)是简单的数据结构,但在计算机中使用广泛.它是有序的元素集合.栈最显 ...

  4. gps转百度地图

    HttpResponse res=WS.url(mapUrl+"/ag/coord/convert?from=0&to=4&x="+longitude+" ...

  5. 上传图片HTML <form> 标签的 method 属性

    <!DOCTYPE HTML><html><body> <form action="/example/html5/demo_form.asp&quo ...

  6. 一天天的sql总结

    一. 多张表之间的查询: join/inner join  on inner join 是比较运算符,只返回符合条件的行. left/outer join on 左外连接包含left join左表所有 ...

  7. TStringList TMemo Text与Add赋值的区别 Memo.Text赋值高度注意事项,不得不知的技巧。

    Memo.Text赋值高度注意事项,不得不知的技巧. list := TStringList.Create;  list.Text:= str:  list.Count; list.Clear;  l ...

  8. Jsoncpp的使用

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  9. 抽象数据类型ADT

    ADT(Abstract Data Type) 类型由什么组成? 一个类型(type)指定两类信息,一个属性集和一个操作集. 假设要定义一个新的数据类型.首先,要提供存储数据的方式,可能是通过设计一个 ...

  10. 在C#中创建和读取XML文件

    1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...