Lightoj 1011 - Marriage Ceremonies
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 jth woman. 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 |
Output for Sample Input |
2 2 1 5 2 1 3 1 2 3 6 5 4 8 1 2 |
Case 1: 7 Case 2: 1 |
n男 n女 选n对使他们的权值最大。相当于每行取一个数且这些数不能有在同一列的。
状态:dp[s]=max(dp[s],dp[s|(1<<i)]+a[cnt][i],其中s&(1<<i)=1
/* ***********************************************
Author :guanjun
Created Time :2016/6/14 19:21:17
File Name :1011.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int dp[<<];
int n,m;
int a[][];
int dfs(int s,int cnt){
if(s==(<<n)-){
return dp[s]=;
}
if(dp[s]!=-)return dp[s];
for(int i=;i<n;i++){
if(!(s&(<<i))){
dp[s]=max(dp[s],dfs(s|(<<i),cnt+)+a[cnt][i]);
}
}
return dp[s];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T;
cin>>T;
for(int t=;t<=T;t++){
cin>>n;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d",&a[i][j]);
memset(dp,-,sizeof dp);
printf("Case %d: %d\n",t,dfs(,)); }
return ;
}
另一种姿势:dp[i][s]=max(dp[i][s],dp[i-1][k]+a[i][j]).第i行状态为s时能得到的最大值。k是上一行的状态。暴力转移一下:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int arr[][];
int dp[][ << ];
int main()
{
int T;
int _case = ;
scanf("%d", &T);
while(T--)
{
memset(dp, , sizeof(dp));
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j < n; j++)
scanf("%d", &arr[i][j]);
for(int i = ; i <= n; i++)
{
for(int j = ; j < n; j++)
{
for(int k = ;k <= ( << n) - ;k++)
{
if( (k & ( << j)) == )
dp[i][k | ( << j)] = max(dp[i][k | ( << j)], dp[i - ][k] + arr[i][j]);
}
}
}
int ans = ;
for(int i = ;i <= ( << n) - ;i++)
ans = max(ans, dp[n][i]);
printf("Case %d: %d\n", _case++, ans);
}
return ;
}
Lightoj 1011 - Marriage Ceremonies的更多相关文章
- 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对相亲,保证都是正常的(无搞基百合之类的), ...
- 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 ...
- 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) ...
随机推荐
- 【51nod 1092】 回文字符串(区间DP)
回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3 ...
- Web安全解决方案
什么是 .NET Framework 安全性? .NET Framework 提供了用户和代码安全模型,允许对用户和代码可以执行的操作进行限制.要对基于角色的安全性和代码访问安全性进行编程,可以从 S ...
- 404 Not Found 由来
404 NOT FOUND! 抱歉,沒有找到您需要的文章!! 什么是 404 Not Found 404页面是网站必备的一个页面,它承载着用户体验与SEO优化的重任.404页面通常为用户访问了网站上不 ...
- functools内置装饰器
def update_wrapper(wrapper, wrapped, assigned = WRAPPER_ASSIGNMENTS, updated = WRAPPER_UPDATES): def ...
- 73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 针对于Spring Boot提供的注解,如果没有好好研究一下的话,那么想应用自如Spring Boot的话,还是有点困难的,所以我们这小节,说说S ...
- 字典树模板题 POJ 2503
#include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void in ...
- 【转】SQL Server 创建约束图解 唯一 主键-界面操作
SQL Server 创建约束图解 唯一 主键-界面操作 SQLServer中有五种约束,Primary Key约束.Foreign Key约束.Unique约束.Default约束和Check约束, ...
- [NOIP2001] 提高组 洛谷P1024 一元三次方程求解
题目描述 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100至100之间),且根与根之差 ...
- BZOJ2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
n<=50000个点的树,求选最多不相邻点的个数. f[i][0]=sigma max(f[j][0],f[j][1]),j为i的儿子 f[i][1]=sigma f[j][0],j同上 死于未 ...
- iOS url带中文下载时 报错解决方法
问题描述:下载文件时, 请求带中文的URL的资源时,比如:http://s237.sznews.com/pic/2010/11/23/e4fa5794926548ac953a8a525a23b6f2/ ...