light oj 1011 - Marriage Ceremonies
题目大意:
给出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的更多相关文章
- Light OJ 1011 - Marriage Ceremonies(状压DP)
题目大意: 有N个男人,和N个女人要互相匹配,每个男人和每个女人有个匹配值. 并且匹配只能是1对1的. 问所有人都匹配完成,最大的匹配值是多少? 状压DP,暴力枚举就OK了, 这个题目略坑,因为他 ...
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- Lightoj 1011 - Marriage Ceremonies
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job ...
- light oj 1184 Marriage Media
题目: You run a marriage media. You take some profiles for men and women, and your task is to arrange ...
- Light OJ 1011
题意: (好难看) 给你 N 个 男的, 女的, 男的选女票, 题目给出矩阵, Mp[i][j] 表示 第 i 个男的选 第 J 个女的优先值 选了 J 之后的就不能选 J 了: 求所有狗男女的最大优 ...
- Lightoj1011 - Marriage Ceremonies
1011 - Marriage Ceremonies PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- (状压) Marriage Ceremonies (lightOJ 1011)
http://www.lightoj.com/volume_showproblem.php?problem=1011 You work in a company which organizes mar ...
- Marriage Ceremonies LightOJ - 1011
Marriage Ceremonies LightOJ - 1011 常规状压dp.popcount(S)表示S集合中元素数量.ans[S]表示S中的女性与前popcount(S)个男性结婚的最大收益 ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- WPF开发的界面调用C++生成的dll文件
以引用d1.dll为例. [生成d1.dll] 文件——新建——项目——Visual C++——Win32项目,选择DLL,点击Finish.在d1.cpp中添加代码 #include "s ...
- springcloud集成zookeeper,并使用configserver作为服务的配置中心
1.springcloud集成zookeeper: 做法: 出现问题: 版本不一致导致出现keepError: 解决:服务器的zookeeper要与客户端的zookeeper一致,才可以. 2.使用c ...
- (编辑距离问题 线性DP) nyoj1431-DNA基因鉴定
题目描述: 我们经常会听说DNA亲子鉴定是怎么回事呢?人类的DNA由4个基本字母{A,C,G,T}构成,包含了多达30亿个字符.如果两个人的DNA序列相差0.1%,仍然意味着有300万个位置不同,所以 ...
- eclipse 中新建文件报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Buil
在eclipse中新建文件报错错误提示如下: The superclass "javax.servlet.http.HttpServlet" was not found on th ...
- (转载)python: getopt的使用;
注: 该文转载于https://blog.csdn.net/tianzhu123/article/details/7655499python中 getopt 模块, 该模块是专门用来处理命令行参数的 ...
- 11款插件让你的Chrome成为全世界最好用的浏览器|Chrome插件推荐
文章来源:知乎 收录于:风云社区(SCOEE)[提供mac软件下载] 更多专题,可关注小编[磨人的小妖精],查看我的文章,也可上[风云社区 SCOEE],查找和下载相关软件资源. (一)综合类: 新买 ...
- social-auth-app-django集成第三方登录
GitHub:https://github.com/python-social-auth/social-app-django 官网文档:http://python-social-auth.readth ...
- eslint相关工具
eslint工具 1. vscode搜索eslint安装,就可以在写代码时报eslint的错误了 2.文件 --> 首选项 --> 设置 --> 选ESLint --> 勾选A ...
- Matplotlib中柱状图bar使用
一.函数原型 matplotlib.pyplot.bar(left, height, alpha=1, width=0.8, color=, edgecolor=, label=, lw=3) 1. ...
- ranger部署文档(记)
目录 概览... 2 1. ranger-admin. 2 2. ranger-user-sync. 2 3. ranger-*-plugins. 2 安装... 3 1 ...