lightoj 1011 - Marriage Ceremonies 详解(状压DP入门题)

6 篇文章 3 订阅


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 ithline 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

 


题意:对于给定的n个man 和n个woman ,给出每对之间的欣赏度,让你来配对,使得n对之间的欣赏度和最高 (n<=16)

思路:一开始拿到这个题,感觉就是个n皇后。。就要求每行每列都只有1个,然后就是在所有方法中,找出一个权最大的方案,然后按照n皇后去写了,果断tle,复杂度n!,想优化也没想出来,记忆化也不会表示,后来知道是状压,这题其实不算状压DP,算是一个dfs+状压记忆化?第一次写状压DP,说一下理解吧,状压DP其实就是把很多状态用二进制表示,n <=16就是一个暗示。。状压DP做的很少,对一些技巧还是不是很理解。。代码里有详解

先贴一个我一开始暴力的代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 20;
int dp[maxn][maxn], book[maxn], max1, n;
void dfs(int i, int ans)
{
    if(i == n+1)
    {
        if(ans > max1) max1 = ans;
        return;
    }
    for(int j = 1; j <= n; j++)
    {
        if(book[j])
            continue;
        book[j] = 1;
        ans += dp[i][j];
        dfs(i+1, ans);
        book[j] = 0;
        ans -= dp[i][j];
    }
}
int main()
{
    int t, ca = 1;
    scanf("%d", &t);
    while(t--)
    {
        memset(book, 0, sizeof(book));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d", &dp[i][j]);
        max1 = -1;
        dfs(1,0);
        printf("Case %d: %d\n",ca++, max1);

    }
    return 0;
}
状压DP代码:

为了便于记忆化,把每个状态都用二进制表示,否则要开始一个16维的数组。。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1 << 16 + 5;
int dp[maxn], a[20][20], n;
int dfs(int s, int row)
{
    if(s == (1<<n)-1)  //如果每一列都有了,就返回0,从后往前递归
        return dp[s] = 0;
    if(dp[s] != -1) //记忆化,回溯的过程一直在不断更新,如果跳出那个for,也就是这个状态所有情况都跑了一遍,也就是最大值已经出现了,自己脑袋递归下就理解了
        return dp[s];
    for(int i = 0; i < n; i++)
    {
        if(!(s & (1<<i)))  //如果这一行没有被用
            dp[s] = max(dp[s], dfs(s|(1<<i), row + 1) + a[row][i]); //就让他跟这行占用了选出一个最大值,这里自行递归就懂了
    }
    return dp[s];
}
int main()
{
    int t, ca = 1;
    scanf("%d", &t);
    while(t--)
    {
        memset(dp, -1, sizeof(dp));
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &a[i][j]);
        printf("Case %d: %d\n", ca++, dfs(0, 0));
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值