Lightoj1011 - Marriage Ceremonies

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

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/5322643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值