Marriage Ceremonies(状态压缩dp)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job is quite hard for you.
The job gets more difficult when people come here and give their bio-data with their preference about opposite gender. Some give priorities to family background, some give priorities to education, etc.
Now your company is in a danger and you want to save your company from this financial crisis by arranging as much marriages as possible. So, you collect N bio-data of men and N bio-data of women. After analyzing quite a lot you calculated the priority index of each pair of men and women.
Finally you want to arrange N marriage ceremonies, such that the total priority index is maximized. Remember that each man should be paired with a woman and only monogamous families should be formed.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains an integer N (1 ≤ n ≤ 16), denoting the number of men or women. Each of the next N lines will contain N integers each. The jth integer in the ith line denotes the priority index between the ith man and jthwoman. All the integers will be positive and not greater than 10000.
Output
For each case, print the case number and the maximum possible priority index after all the marriages have been arranged.
Sample Input
2
2
1 5
2 1
3
1 2 3
6 5 4
8 1 2
Sample Output
Case 1: 7
Case 2: 16
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[][] ;
int dp[][ << ] ;
int n ; int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
int T ;
scanf ("%d" , &T ) ;
int ans = ;
while (T--) {
scanf ("%d" , &n ) ;
for (int i = ; i < n ; i++) {
for (int j = ; j < n ; j++) {
scanf ("%d" , &a[i][j]) ;
}
}
for (int i = ; i < n ; i++) {
for (int j = ; j < << n ; j++ ) {
dp[i][j] = - ;
}
}
for (int i = ; i < n ; i++) {
dp[][ << i] = a[][i] ;
}
for (int i = ; i < n ; i++) {
for (int j = ; j < << n ; j++) {
if (dp[i - ][j] != -) {
for (int k = ; k < n ; k++) {
if ( ! (j & << k) ) {
dp[i][j | << k] = max (dp[i][j | << k] , dp[i - ][j] + a[i][k] ) ;
}
}
}
}
}
printf ("Case %d: %d\n" , ++ans , dp[n - ][( << n) - ] ) ;
}
return ;
}
先把“ 数据 ”存入 下标为 1 << 0 , 1 << 1 , 1 << 2 , 1 << n - 1的这几个位置,然后就是一般的dp过程了
状态压缩dp我觉得有两点蛮神奇的(当然能用数学方法证明的)
:
1,! ( j & 1 << k ) 这能让已经加过的滚蛋;orz
2,(1 << 0) + (1 << 1 ) + ... + (1 << n - 1) = (1 << n ) - 1 orz 不仔细看还真没发觉
Marriage Ceremonies(状态压缩dp)的更多相关文章
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- 移动APP为什么要开发两套Android和IOS-桥接模式
一.前言 现在用H5开发个 web app 多么方便,兼容两大系统Andriod和IOS.但是为什么许多公司还要开发原生的APP?开发原生的APP就需要开发两套一套运行在Andriod系统的,一套运行 ...
- 【转】十分详细的xStream解析
转自博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html xStream框架 xStream可以轻易的将Java对象和xml文 ...
- 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- 随堂练习——Rational rose
管理员 学生
- 读JS高级——第五章-引用类型 _记录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- poj3580 伸展树(区间翻转 区间搬移 删除结点 加入结点 成段更新)
好题.我做了很久,学了大牛们的区间搬移.主要的代码都有注释. #include<cstdio> #include<cstring> #include<iostream&g ...
- Spring控制Hibernate的缓存机制ehcache
首先在spring.xml中进入bean <prop key="hibernate.cache.use_second_level_cache">true</pro ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- spring c3p0数据库连接池连接配置
c3p0连接池配置 xml文件内容如下: C3P0 通过这些属性,可以对数据源进行各种有效的控制 lc_biz_datasource_c3p0.properties 配置: lc_biz_dataso ...
- POJ2226 Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10149 Accepted: 3783 Description Rain ...