lightoj-1011 - Marriage Ceremonies(状压dp)

Posted FireCool

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lightoj-1011 - Marriage Ceremonies(状压dp)相关的知识,希望对你有一定的参考价值。

1011 - Marriage Ceremonies
PDF (English) Statistics Forum
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

 

解题思路:用dp[i][j] 来表示,其中i是表示计算到第几层,j的二进制中的1表示已经取了那些位置的数字

状态转移方程dp[i][j|(1<<k)] = max(dp[i][j],dp[i-1][j],map[i-1][k]);

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring> 
#include<algorithm>
using namespace std;

int map[20][20];
int dp[20][65635];
int T,n,len;

int main(){
    
    scanf("%d",&T);
    
    for(int t=1;t<=T;t++){
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        len = (int)pow(2,n);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&map[i][j]);
        
        for(int i=1;i<=n;i++){
            
            for(int j=0;j<n;j++){
                
                for(int k=0;k<len;k++){
                    if(((1<<j)&k)) continue;
                    dp[i][(1<<j)|k] = max(dp[i][(1<<j)|k],dp[i-1][k]+map[i-1][j]);
                }
                
            }
            
        }
        printf("Case %d: %d\n",t,dp[n][len-1]);
        
    }
    
    
    return 0;
} 

 

以上是关于lightoj-1011 - Marriage Ceremonies(状压dp)的主要内容,如果未能解决你的问题,请参考以下文章

lightoj-1011 - Marriage Ceremonies(状压dp)

Marriage Ceremonies LightOJ - 1011

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

HDU-3081 Marriage Match II (最大流,二分答案,并查集)

HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流

Marriage is Stable