题目大意:

给出n*n的矩阵Map,Map[i][j]代表第i个男人和第j个女人之间的满意度,求男女一一配对后,最大的满意度之和。

题目思路:状态压缩

题目可看做每行取一点,所有点不同列的情况下,各个点的最大和为多少。

dp[i][j],代表第i行,状态为j的情况下的最优解,其中j的含义为:j所代表的二进制数中:若第i位为1则取第i列的值,为0则不取。

这样j从1到(1<<n)-1,便包含了矩阵中所有列的取法

为了节约时间,我们需要作出一点优化:我们知道i*i的矩阵中不可能存在比i大的列,也就是说(i&(1<<k)应该大于0,k从0到n-1)。

得出状态转移方程式:dp[i][j]=max(dp[i][j],dp[i-1][j^(1<<k)]+Map[i][k+1])

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define LL long long
#define MAXSIZE 1005
using namespace std; int dp[][],Map[MAXSIZE][MAXSIZE]; int Solve(int n)
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<(<<n);j++)
{
int cnt=;
for(int k=;k<n;k++)//优化时间
{
if(j&(<<k)) cnt++;
}
if(cnt!=i)
continue;
for(int k=;k<n;k++)
if(j&(<<k))
dp[i][j]=max(dp[i][j],dp[i-][j^(<<k)]+Map[i][k+]);
}
}
return dp[n][(<<n)-];
} int main()
{
int T,cns=,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&Map[i][j]);
int ans=Solve(n);
printf("Case %d: %d\n",cns++,ans);
}
return ;
}

light oj 1011 - Marriage Ceremonies的更多相关文章

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

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

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

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

  3. Lightoj 1011 - Marriage Ceremonies

    You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job ...

  4. light oj 1184 Marriage Media

    题目: You run a marriage media. You take some profiles for men and women, and your task is to arrange ...

  5. Light OJ 1011

    题意: (好难看) 给你 N 个 男的, 女的, 男的选女票, 题目给出矩阵, Mp[i][j] 表示 第 i 个男的选 第 J 个女的优先值 选了 J 之后的就不能选 J 了: 求所有狗男女的最大优 ...

  6. Lightoj1011 - Marriage Ceremonies

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

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

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

  8. Marriage Ceremonies LightOJ - 1011

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

  9. Marriage Ceremonies(状态压缩dp)

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

随机推荐

  1. qml:基本知识记录

    1.  property信号处理函数: 在qml中,通过property可以定义属性,这些属性自带信号处理函数,例如: property   string    szTitle: “hello wor ...

  2. 2017-12-15python全栈9期第二天第七节之数字转换成布尔值

    #!/user/bin/python# -*- coding:utf-8 -*-print(bool(2))

  3. linux对4T硬盘进行分区

    使用parted工具: yum install parted parted /dev/sdb //选择硬盘 GNUParted 2.3Using /dev/sdbWelcome to GNU Part ...

  4. Windows 运行库

    Microsoft Visual C++ 2005 Redistributable - 8.0.61001http://download.microsoft.com/download/8/B/4/8B ...

  5. Ubuntu 云服务器上部署自己的 Rails 应用

    自学rails一段时间了,之前只用heroku部署了网站,想尝试把网站以一个更“正经”的方式呈现出来,就买了一个阿里云服务器.参考了网上部分rails部署教程,过程中也遇到了一些问题,所以在完成之后总 ...

  6. 解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 问题

    详细报错如下 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.17 from http://maven ...

  7. vs code配置git

    在项目目录执行 git init 修改.git文件夹下的config文件 [core] repositoryformatversion = 0 filemode = false bare = fals ...

  8. HDU 1097(m次幂的个位数 规律)

    题意是求 n^m 结果的最后一位数. 可以用快速幂取模的方法做,当然本题还有更简单的方法: 所有数字( 0 - 9 )的 m 次幂的个位数不会受进位的影响,只收到乘数的影响,所以在结果中一旦出现之前出 ...

  9. HDU 1284(钱币兑换 背包/母函数)

    与 HDU 1028 相似的题目. 方法一:完全背包. 限制条件:硬币总值不超过 n. 目标:求出组合种数. 令 dp[ i ][ j ] == x 表示用前 i 种硬币组合价值为 j 的钱共 x 种 ...

  10. Spring Boot中使用使用Spring Security和JWT

     目标 1.Token鉴权 2.Restful API 3.Spring Security+JWT 开始 自行新建Spring Boot工程 引入相关依赖 <dependency> < ...