Lightoj 1011 - Marriage Ceremonies

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对使他们的权值最大。相当于每行去一个数且这些数不能有在同一列的。

/* ***********************************************
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 << 61;
const double eps=1e-5;
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[1<<16];
int n,m;
int a[30][30];
int dfs(int s,int cnt){
    if(s==(1<<n)-1){
        return dp[s]=0;
    }
    if(dp[s]!=-1)return dp[s];
    for(int i=0;i<n;i++){
        if(!(s&(1<<i))){
            dp[s]=max(dp[s],dfs(s|(1<<i),cnt+1)+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=1;t<=T;t++){
        cin>>n;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&a[i][j]);
        memset(dp,-1,sizeof dp);
        printf("Case %d: %d\n",t,dfs(0,0));

    }
    return 0;
}
时间: 2024-10-26 02:31:17

Lightoj 1011 - Marriage Ceremonies的相关文章

lightoj 1011 Marriage Ceremonies (暴力)(记忆化dp,状压) 转

转载自:http://blog.csdn.net/xindoo/article/details/9173949 暴力TLE 1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int vis[17]; 8 int n, ans, sum; 9 int map[19][19]; 10 11 void dfs(int x) 12 { 1

lightoj-1011 - Marriage Ceremonies(状压dp)

1011 - Marriage Ceremonies PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBYou 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 difficul

(状压) Marriage Ceremonies (lightOJ 1011)

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 bi

LightOJ 1184 - Marriage Media 【二分图最大匹配】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1184 根据一些要求建图即可 代码: #include <iostream> #include <algorithm> #include <set> #include <map> #include <string.h> #include <queue> #include <sstream> #includ

LightOJ1011---Marriage Ceremonies (状压dp)

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 gi

UVa11020 &#183; Efficient Solutions

题目:http://uva.onlinejudge.org/external/110/11020.pdf Problem IEfficient SolutionsInput: Standard Input Output: Standard Output "Our marriage ceremonies are solemn, sobermoments of reflection; also regret, disagreement,argument and mutual recriminatio

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

LightOJ 1427 Substring Frequency (II) (AC自动机)

题意:给定一个文本串和 n 个子串,问你子串在文本串出现的次数. 析:很明显的AC自动机,只要把先把子串进行失配处理,然后再去用文本串去匹配,在插入子串时就要标记每个串,注意串可能是相同的,这个我错了两次,最后匹配一次就OK了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib