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) ...
随机推荐
- Fairy Tail - Main Theme Slow Version guitar (solo)
Хвост Феи animelodies1 (on youtube) Переписал jarrro (on vk.com)
- reviews of learn python3 the hard way
Almost every time,I try my best to write a long review of the book I have read. But this time I want ...
- 常见mysql分布式数据中间件
一般分为三种: 1. proxy sharding,目前由cobar,mycat,drds,atlas修改,这几个产品的起源一般是mysqlproxy 或 ameoba,特点是mysql协议基本兼容, ...
- VS 预先生成事件命令
宏 说明 $(ConfigurationName) 当前项目配置的名称(例如,“Debug|Any CPU”). $(OutDir) 输出文件目录的路径,相对于项目目录.这解析为“输出目录”属性的值. ...
- Statelessness Provide credentials with the request. Each request MUST stand alone and should not be affected from previous conversation happened from same client in past.
The server never relies on information from previous requests. Statelessness As per the REST (REpres ...
- 通过定时任务 bash 脚本 控制 进程 的 执行时间
通过定时任务 bash 脚本 控制 进程 的 执行时间
- 使用C语言获取字符串或文件的MD5值
libmd5地址:https://sourceforge.net/projects/libmd5-rfc/ MD5Demo1.c #include <stdio.h> #include & ...
- Java,多个线程对同一个数据源的访问
当多个线程对同一个数据源进行访问时,应对线程同步或加锁.为何?举个简单的例子:有一个共享的数据源dataSource,其值为0.有两个线程,Thread1和Thread2.Thread1的任务是将da ...
- iOS开发中对于一些常用的相对路径(持续更新)
1.iOS开发的证书的描述文件放置地点 ~/Library/MobileDevice/Provisioning Profiles 2.$(SRCROOT)代表的是这个项目文件夹所在的位置 $(PR ...
- id、NSObject *、id<NSObject>、instancetype
1. id 与 NSObject * (1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的 ...