Marriage Ceremonies

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

Description

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

2

2

1 5

2 1

3

1 2 3

6 5 4

8 1 2

Sample Output

Case 1: 7

Case 2: 16

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[][] ;
int dp[][ << ] ;
int n ; int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
int T ;
scanf ("%d" , &T ) ;
int ans = ;
while (T--) {
scanf ("%d" , &n ) ;
for (int i = ; i < n ; i++) {
for (int j = ; j < n ; j++) {
scanf ("%d" , &a[i][j]) ;
}
}
for (int i = ; i < n ; i++) {
for (int j = ; j < << n ; j++ ) {
dp[i][j] = - ;
}
}
for (int i = ; i < n ; i++) {
dp[][ << i] = a[][i] ;
}
for (int i = ; i < n ; i++) {
for (int j = ; j < << n ; j++) {
if (dp[i - ][j] != -) {
for (int k = ; k < n ; k++) {
if ( ! (j & << k) ) {
dp[i][j | << k] = max (dp[i][j | << k] , dp[i - ][j] + a[i][k] ) ;
}
}
}
}
}
printf ("Case %d: %d\n" , ++ans , dp[n - ][( << n) - ] ) ;
}
return ;
}

先把“ 数据 ”存入 下标为 1 << 0 , 1 << 1 , 1 << 2 , 1 << n - 1的这几个位置,然后就是一般的dp过程了

状态压缩dp我觉得有两点蛮神奇的(当然能用数学方法证明的)

1,! ( j & 1 << k ) 这能让已经加过的滚蛋;orz

2,(1 << 0) + (1 << 1 ) + ... + (1 << n - 1) = (1 << n ) - 1 orz 不仔细看还真没发觉

Marriage Ceremonies(状态压缩dp)的更多相关文章

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

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

  2. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  3. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  4. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  5. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  6. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  7. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  8. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. UEditor编辑文章出现多余空行问题的解决办法

    网站后台管理编辑文章以后,保存.再次打开编辑是文章前后多出多余的空行.一直以为是编辑器有问题,也没在意,但是自己编辑文章多了,感觉太麻烦了.想一定可以解决这个问题.百度一下果然找到原因.原来是加载编辑 ...

  2. Bootstrap系列 -- 25. 下拉菜单分割线

    在Bootstrap框架中的下拉菜单还提供了下拉分隔线,假设下拉菜单有两个组,那么组与组之间可以通过添加一个空的<li>,并且给这个<li>添加类名“divider”来实现添加 ...

  3. 要期末了搞不了OI了额……

    TAT~~~~~~期中完挂,求RP求期末逆袭

  4. [USACO2006][poj3182]The Grove(巧妙的BFS)

    题目:http://poj.org/problem?id=3182 题意:一个棋盘中间有一个联通块,给你一个起点让你从起点开始绕联通块外围一圈并回到起点,求最小步数. 分析: 首先根据数据的范围比较小 ...

  5. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  6. 8、面向对象以及winform的简单运用(事件与winform入门)

    事件 Visual studio中对可视化窗体控件的事件处理机理: 所有的.NET Framework可视化窗体控件的预定义事件,都会某一对应的“事件名+Handler”委托类型的变量.与此事件相关的 ...

  7. RestFul API初识

    python Restful API 微博开放平台: open.weibo.com: 点击文档进入API查看界面 点击API文档进行查看: 比如点开粉丝数的API可以看到: pro.jsonlint. ...

  8. centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记

    centos 6.5下安装mysql+nginx+redmine 3.1.0 笔记 目录[-] 过程 1.安装RVM 2.利用rvm安装 Ruby 1.9.3 并设为默认 3.安装rails 4.安装 ...

  9. 百度CDN公共库

    百度CDN公共库 后续可以直接调用 地址:http://developer.baidu.com/wiki/index.php?title=docs/cplat/libs jQuery 加载地址: 未压 ...

  10. .Net身份验证概述

    一直以来,所有的系统基本都会有用户的登陆验证过程,整个过程其实也不难理解,就是对于cookie的解析.微软的.Net平台围绕用户身份验证授权也有好几个版本了,从早期的Membership到Identi ...