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) ...
随机推荐
- Fiddler抓包-只抓APP的请求
from:https://www.cnblogs.com/yoyoketang/p/6582437.html fiddler抓手机app的请求,估计大部分都会,但是如何只抓来自app的请求呢? 把来自 ...
- Leetcode 260.只出现一次的数字III
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5 ...
- TJOI2014
匹配 给出一个\(n(n\leq80)\)个点对\(n\)个点的带权二分图,求所有最大权匹配的交集. 先求出一个最大权匹配,然后枚举每一条匹配中的边,检验删除该边后是否还能形成最大权匹配.如果能则说明 ...
- 【BZOJ1403】Divisibility Testing(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<algorithm> #include<map> ...
- IE下IFrame引用跨域站点页面时,Session失效问题解决
问题场景:在一个应用(集团门户)的某个page中, 通过IFrame的方式嵌入另一个应用(集团实时监管系统)的某个页面. 当两个应用的domain 不一样时, 在被嵌入的页面中Session失效.(s ...
- Milking Time---poj3616
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- mybatis结合generator进行分页插件PluginAdapter开发
使用org.mybatis.generator生成UserExample时,无法进行分页,使用下面这个类运行generator便可以生成分页相关的属性了 package org.mybatis.gen ...
- Mybatis各种模糊查询(转)
模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...
- 我在使用eclipse配置Tomcat服务器的时候发现,默认情况下Tocmat把我们部署的项目放在了workspaces下面,而不是像Myeclipse默认的那样放在tomcat的安装路径下。
1.我在使用eclipse配置Tomcat服务器的时候发现,默认情况下Tocmat把我们部署的项目放在了workspaces下面,而不是像Myeclipse默认的那样放在tomcat的安装路径下. 2 ...
- Go --- 设计模式(模板模式)
模版模式真的是一个好东西.所谓模版模式,就是说,某几个类中相同的操作和代码提取到父类的一个函数中,并定义相同的操作为抽象函数.由子类来实现.估计我也没表达清楚,下面还是看代码来讲解吧. 例:我们有两个 ...