Marriage Ceremonies LightOJ - 1011
Marriage Ceremonies LightOJ - 1011
常规状压dp。popcount(S)表示S集合中元素数量。ans[S]表示S中的女性与前popcount(S)个男性结婚的最大收益。
那么,$ans[S]=max\{ans[S-p]+a[popcount(S)][p]\}$
老代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans[][],a[][],T,TT,n;
int main()
{
int i,j,k;
scanf("%d",&T);
for(TT=;TT<=T;TT++)
{
scanf("%d",&n);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&a[i][j]);
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
for(j=;j<(<<n);j++)
{
if(__builtin_popcount(j)!=i) continue;
for(k=;k<=n;k++)
if(j&(<<(k-)))
ans[i][j]=max(ans[i][j],ans[i-][j^(<<(k-))]+a[i][k]);
}
printf("Case %d: %d\n",TT,ans[n][(<<n)-]);
}
return ;
}
新代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans[],a[][],T,TT,n;
int main()
{
int i,j,k;
scanf("%d",&T);
for(TT=;TT<=T;TT++)
{
scanf("%d",&n);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&a[i][j]);
memset(ans,,sizeof(ans));
for(i=;i<(<<n);i++)
for(j=;j<=n;j++)
if(i&(<<(j-)))
ans[i]=max(ans[i],ans[i^(<<(j-))]+a[__builtin_popcount(i)][j]);
printf("Case %d: %d\n",TT,ans[(<<n)-]);
}
}
另:貌似可以用二分图最大完美匹配做
Marriage Ceremonies LightOJ - 1011的更多相关文章
- (状压) Marriage Ceremonies (lightOJ 1011)
http://www.lightoj.com/volume_showproblem.php?problem=1011 You work in a company which organizes mar ...
- Lightoj 1011 - Marriage Ceremonies
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Lightoj1011 - Marriage Ceremonies
1011 - Marriage Ceremonies PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- Light OJ 1011 - Marriage Ceremonies(状压DP)
题目大意: 有N个男人,和N个女人要互相匹配,每个男人和每个女人有个匹配值. 并且匹配只能是1对1的. 问所有人都匹配完成,最大的匹配值是多少? 状压DP,暴力枚举就OK了, 这个题目略坑,因为他 ...
- light oj 1011 - Marriage Ceremonies
题目大意: 给出n*n的矩阵Map,Map[i][j]代表第i个男人和第j个女人之间的满意度,求男女一一配对后,最大的满意度之和. 题目思路:状态压缩 题目可看做每行取一点,所有点不同列的情况下,各个 ...
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- lightoj 1011 最大权重匹配或最大费用流
由于暂时不会KM算法,只能用最大费用流来做了. 题目链接:http://lightoj.com/volume_showproblem.php?problem=1011 #include <cst ...
- lightoj 1011 (状态压缩dp)
思路:状态压缩dp,设dp[i][j] 表示前i行,状态为j时的最大值,状态定义为:若前i行中取了第x列那么j的二进制位中第x位为1,否则为0,最后答案就是dp[n-1][(1 << n) ...
随机推荐
- Python 003- 小知识汇总(更新中)
#查询key是否存在,可以在使用未知的字典的时候使用 #-*- coding:utf-8 -*- D={'a':1,'c':3,'b':2} for key in sorted(D): print(k ...
- Gym - 101164C - Castle KMP
题目链接:传送门 题解: 利用KMP的fail失配数组,快速找到当前后缀与前缀的公共前缀点 #include<bits/stdc++.h> using namespace std; #pr ...
- camus gobblin
####Camus is being phased out and replaced by Gobblin. For those using or interested in Camus, we su ...
- JSON和JavaScript对象
var obj={width:100,height:200},这样的并不叫JSON,并且JSON只是一种数据格式,并不是具体的实例. 但很多人把这样的JS对象当成JSON,下面把这个问题讲清楚 一.J ...
- luogu2704 炮兵阵地 状态压缩DP
题目大意:一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),在每一格平原地形上最多可以布置一支炮兵部队,能攻击到的区域:沿横向左右各两格,沿纵向上 ...
- (31)java web的hibernate使用-一级缓存,二级缓存
参考:https://blog.csdn.net/miachen520/article/details/52195832 hibernate自带一级缓存 和 二级缓存 一,一级缓存: 基于Sessio ...
- redis04-----Hash 哈希数据类型相关命令
Hash 哈希数据类型相关命令 hset key field value 这里的域就是键值对的键. 作用: 把key中 filed域的值设为value 注:如果没有field域,直接添加,如果有,则覆 ...
- react项目中的注意点
一.ES6 的编译方法 目前主流的浏览器还不支持ES6. 现在一般采用webpack 和 <script type="text/babel">对jsx 语法进行编译, ...
- Linux 高精度定时器hrtimer 使用示例【转】
本文转载自:http://blog.csdn.net/dean_gdp/article/details/25481225 hrtimer的基本操作 Linux的传统定时器通过时间轮算法实现(timer ...
- 一步一步学Silverlight 2系列(16):数据与通信之JSON
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...