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

n男  n女 选n对使他们的权值最大。相当于每行取一个数且这些数不能有在同一列的。

状态:dp[s]=max(dp[s],dp[s|(1<<i)]+a[cnt][i],其中s&(1<<i)=1

/* ***********************************************
Author :guanjun
Created Time :2016/6/14 19:21:17
File Name :1011.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dp[<<];
int n,m;
int a[][];
int dfs(int s,int cnt){
if(s==(<<n)-){
return dp[s]=;
}
if(dp[s]!=-)return dp[s];
for(int i=;i<n;i++){
if(!(s&(<<i))){
dp[s]=max(dp[s],dfs(s|(<<i),cnt+)+a[cnt][i]);
}
}
return dp[s];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T;
cin>>T;
for(int t=;t<=T;t++){
cin>>n;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d",&a[i][j]);
memset(dp,-,sizeof dp);
printf("Case %d: %d\n",t,dfs(,)); }
return ;
}

另一种姿势:dp[i][s]=max(dp[i][s],dp[i-1][k]+a[i][j]).第i行状态为s时能得到的最大值。k是上一行的状态。暴力转移一下:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int arr[][];
int dp[][ << ];
int main()
{
int T;
int _case = ;
scanf("%d", &T);
while(T--)
{
memset(dp, , sizeof(dp));
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j < n; j++)
scanf("%d", &arr[i][j]);
for(int i = ; i <= n; i++)
{
for(int j = ; j < n; j++)
{
for(int k = ;k <= ( << n) - ;k++)
{
if( (k & ( << j)) == )
dp[i][k | ( << j)] = max(dp[i][k | ( << j)], dp[i - ][k] + arr[i][j]);
}
}
}
int ans = ;
for(int i = ;i <= ( << n) - ;i++)
ans = max(ans, dp[n][i]);
printf("Case %d: %d\n", _case++, ans);
}
return ;
}

Lightoj 1011 - Marriage Ceremonies的更多相关文章

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

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

  2. light oj 1011 - Marriage Ceremonies

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

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

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

  4. Lightoj1011 - Marriage Ceremonies

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

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

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

  6. Marriage Ceremonies LightOJ - 1011

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

  7. Marriage Ceremonies(状态压缩dp)

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

  8. lightoj 1011 最大权重匹配或最大费用流

    由于暂时不会KM算法,只能用最大费用流来做了. 题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 #include <cst ...

  9. lightoj 1011 (状态压缩dp)

    思路:状态压缩dp,设dp[i][j] 表示前i行,状态为j时的最大值,状态定义为:若前i行中取了第x列那么j的二进制位中第x位为1,否则为0,最后答案就是dp[n-1][(1 << n) ...

随机推荐

  1. sqlserver复制表数据到另一个表

    SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Server中,,如果目标表不存在: select * into 目标表 from ...

  2. PTA 05-树9 Huffman Codes (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/671 5-9 Huffman Codes   (30分) In 1953, David ...

  3. HDU 3062 简单的2-SAT问题

    在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){    int m=2*i+a;    int n=2*j+b;  ...

  4. hdu 2167 状态压缩

    /*与1565的解法差不多*/ #include<stdio.h> #include<string.h> int map[16][16]; int dp[2][1<< ...

  5. JavaScript 将行结构数据转化为树结构数据源(高效转化方案)

    js接收到后台的数据如下 /// 部门信息 var departRows = [{ parentDepartId: 'root', departId: 'DC', departName: '集团' } ...

  6. Codeforces 659E New Reform【DFS】

    题目链接: http://codeforces.com/problemset/problem/659/E 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? 分析: 无 ...

  7. foobar2000 iOS使用,并连接PC的歌曲进行播放

    foobar2000移动版下载地址:http://mobile.foobar2000.com/ 要实现歌曲互通有两种方法,使用iOS客户端的FTP Server用PC上传歌曲到iOS手机,和在PC上使 ...

  8. IE浏览器不能上传图片

    这时将弹出一个“安全设置-Internet选项”对话框,把右侧滚动条慢慢地往下拉. 找到“其他/将文件上载到服务器包含本地目录路径”点击下面的“启用”功能

  9. jQuery的一些总结(持续更新中...)

    本文为原创,转载请注明出处: cnzt       文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6693399.html 1. $.expr[':']  过滤 ...

  10. 【APUE】孤儿进程与僵死进程

    基本概念: 在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程.子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束. 当一个 进程 ...