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[i][j]表示前i个人选,在状态j时的最大值,状态转移方程   dp[i][j]=max(dp[i][j],dp[i-1][z]+ma[i][u]);u表示当前i这一个位置选的是ma[i][u]那个点。
由于 我想先把表打出来,也就是记录每个i,的j的各个状态的表,然后超内存,之后dp改成滚动数组还超,所以我就先把5到16的表打出来,然后前面按不打表的方式循环,后面直接查表,A了。时间复杂度也小了,大概为(n*(1<<n));
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<vector>
7 using namespace std;
8 typedef long long LL;
9 typedef unsigned long long ll;
10 int dp[2][1<<16];
11 int ma[17][17];
12 typedef struct pp
13 {
14 int x;
15 int id;
16 } ss;
17 vector<ss>vec[12][1<<16];
18 int main(void)
19 {
20 int i,j,k,p,q;
21 int s;
22 for(i=5; i<=16; i++)
23 {
24 for(j=(1<<(i-1)); j<1<<16; j++)
25 {
26 int ans=0;
27 int uu=j;
28 while(uu)
29 {
30 if(uu%2)ans++;
31 uu/=2;
32 }
33 if(ans==i)
34 {
35 for(s=1; s<=16; s++)
36 {
37 if(((1<<(s-1))&j))
38 {
39 ss dd;
40 dd.x=(1<<(s-1)^j);
41 dd.id=s;
42 vec[i-5][j].push_back(dd);
43 }
44 }
45 }
46 }
47 }
48 scanf("%d",&k);
49 for(s=1; s<=k; s++)
50 {
51 scanf("%d",&p);
52 for(i=1; i<=p; i++)
53 {
54 for(j=1; j<=p; j++)
55 {
56 scanf("%d",&ma[i][j]);
57 }
58 }
59 int u;
60 int maxx=0;
61 memset(dp,0,sizeof(dp));
62 for(i=1; i<=4&&i<=p; i++)
63 { int k=i%2;
64 for(j=(1<<(i-1)); j<(1<<p); j++)
65 {
66 for(u=1; u<=p; u++)
67 {
68 if((1<<(u-1))&j)
69 {
70 int z=(1<<(u-1))^j;
71 dp[k][j]=max(dp[k][j],dp[(k+1)%2][z]+ma[i][u]);
72 maxx=max(dp[k][j],maxx);
73 }
74 }
75 }
76 }
77 for(i=5; i<=(p); i++)
78 {
79 int k=i%2;
80 for(j=(1<<(i-1)); j<(1<<p); j++)
81 {
82 for(u=0; u<vec[i-5][j].size(); u++)
83 {
84 ss kk=vec[i-5][j][u];
85 if(kk.id<=p)
86 {
87 dp[k][j]=max(dp[k][j],dp[(k+1)%2][kk.x]+ma[i][kk.id]);
88 maxx=max(dp[k][j],maxx);
89 }
90 }
91 }
92 }
93 printf("Case %d: ",s);
94 printf("%d\n",maxx);
95 }
96 return 0;
97 }
 
 

 
 


Problem Setter: Jane Alam Jan
Developed and Maintained by
JANE ALAM JAN
Copyright 2012

LightOJ, Jane Alam Jan

Lightoj1011 - Marriage Ceremonies的更多相关文章

  1. (状压) Marriage Ceremonies (lightOJ 1011)

    http://www.lightoj.com/volume_showproblem.php?problem=1011 You work in a company which organizes mar ...

  2. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. Marriage Ceremonies LightOJ - 1011

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

  4. Lightoj 1011 - Marriage Ceremonies

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

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

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

  6. light oj 1011 - Marriage Ceremonies

    题目大意: 给出n*n的矩阵Map,Map[i][j]代表第i个男人和第j个女人之间的满意度,求男女一一配对后,最大的满意度之和. 题目思路:状态压缩 题目可看做每行取一点,所有点不同列的情况下,各个 ...

  7. light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)

    题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...

  8. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  9. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

随机推荐

  1. 15. Linux提取RPM包文件(cpio命令)详解

    在讲解如何从 RPM 包中提取文件之前,先来系统学习一下 cpio 命令.cpio 命令用于从归档包中存入和读取文件,换句话说,cpio 命令可以从归档包中提取文件(或目录),也可以将文件(或目录)复 ...

  2. Flume(一)【概述】

    目录 一.Flume定义 二.Flume基础架构 1.Agent 2.Source 3.Sink 4.Channel 5.Event 一.Flume定义 ​ Flume是Cloudera公司提供的一个 ...

  3. 零基础学习java------day5------do....while循环、嵌套、方法(函数)

    1  do...while循环 格式 初始化语句; do { 循环体语句; 控制条件语句; }while(判断条件语句); 流程: 先执行初始化语句 再执行循环体语句 再执行条件控制语句 再做条件的判 ...

  4. Set、Map、WeakSet 和 WeakMap 的区别

    先总结: Set1.  成员不能重复2. 只有健值,没有健名,有点类似数组.3. 可以遍历,方法有add, delete,hasweakSet 1. 成员都是对象 2. 成员都是弱引用,随时可以消失. ...

  5. 连接查询条件在on后面和条件在where后面

    emp表结构如下: dept表结构如下: 内连接 条件语句放在on 后面和 where 结果对于inner join结果是一样的 但对于left join 结果会产生不一样 这种现象也比较好理解,如果 ...

  6. gitlab之数据备份恢复

    备份#备份的时候,先通知相关人员服务要听 ,停止两个服务,并影响访问 root@ubuntu:/opt/web1# gitlab-ctl stop unicorn ok: down: unicorn: ...

  7. 【力扣】146. LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果关键字 (key) ...

  8. Java分层思想

    从最常规的分层结构来说,系统层次从上到下依次为: 表现层/UI层/界面层:主要是客户端的展示. 服务层/业务层:直接为客户端提供的服务或功能.也是系统所能对外提供的功能. 领域层:系统内的领域活动. ...

  9. Mysql配置文件 16c64g优化

    目录 一.说明 二.配置 一.说明 以下配置适合16核64G及以上的配置,会让性能稍微提高1/3左右. 二.配置 my.cnf [client] port = 3306 socket = /usr/l ...

  10. centos7部署mysql-5.7

    目录 一.环境声明 二.程序部署 三.更改初始密码 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql ...