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) ...
随机推荐
- ASP.NET MVC判断基于Cookie的Session过期
当我们第一次请求访问时,可以看到Response的Set-Cookie里添加了ASP.NET_SessionId的值,以后再访问时可以看到Resquest里的Cookie已经包含这个Key. Se ...
- CentOS笔记-目录结构(转载了菜鸟教程里的)
在linux系统中,有几个目录是比较重要的,平时需要注意不要误删除或者随意更改内部文件. /etc: 上边也提到了,这个是系统中的配置文件,如果你更改了该目录下的某个文件可能会导致系统不能启动. /b ...
- (30)java web的hibernate使用-c3p0连接池配置
hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...
- 织梦CMS首页、列表页文章如何调出该文章TAG标签?
1.如果是dedecms v5.7版本直接使用标签 [field:id function=GetTags(@me)/] 就可以调用出来了.只不过不带连接的. 2.如果需要连接请注释掉include/h ...
- HashSe、LinkedHashSet、TreeSet(java基础知识十七)
1.HashSet存储字符串并遍历 * 特点:无序.无索引.无重复 HashSet存储字符串并遍历 HashSet<String> hs = new HashSet<>(); ...
- STL Algorithms 之 unique
C++的文档中说,STL中的unique是类似于这样实现的: template <class ForwardIterator> ForwardIterator unique ( Forwa ...
- golang OOP面向对象
摘自:http://www.01happy.com/golang-oop/ golang中并没有明确的面向对象的说法,实在要扯上的话,可以将struct比作其它语言中的class. 类声明 1 2 3 ...
- hdu2544 迪杰斯特拉题目优化
点击打开题目链接 迪杰斯特拉的用法不多讲,详见 点击打开链接 . 下面两个代码: 这个是用邻接矩阵存图的迪杰斯特拉. #include<stdio.h> int main() { int ...
- CollectionView网格布局
说句老实话,UICollectionView真的太强大了,而且要掌握高级部分是相当困难的.至少笔者是这么认为的,如果觉得自己比较厉害,可以轻而易举地掌握UICollectionView的使用的,希望可 ...
- bzoj 4289 TAX —— 点边转化
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 把边转化成点,同一个原有点相连的边中,边权小的向大的连差值的边,大的向小的连0的边: ...