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) ...
随机推荐
- CTO俱乐部官方群聊-探讨创业和跳槽
今天,CTO俱乐部官方群,有交流,若干活跃分子探讨了创业和跳槽等相关话题. 感觉质量很不错,就整理了下. 老徐 17:02:00 跳来跳去也不是长久之计,除了涨点工资 张苗苗 17:02:46 ...
- Leetcode 273.整数转换英文表示
整数转换英文表示 将非负整数转换为其对应的英文表示.可以保证给定输入小于 231 - 1 . 示例 1: 输入: 123 输出: "One Hundred Twenty Three" ...
- POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K http://poj.org/problem?id=238 ...
- DataTable 转JSON数据
/// <summary> /// 将datatable转换为json /// </summary> /// <param name="dtb"> ...
- ssh 监听多个端口
修改sshd的配置文件 默认位置:/etc/ssh/sshd_config 注释掉 Port 这行 然后添加 ListenAddress 行 e.g: ListenAddress 192.168 ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- hdu 1325数据弱
#include<stdio.h>//判断是否有环,判断是否有点,判断是否是一个父节点 #include<string.h> #define N 1000000 int pre ...
- svg学习之旅(3)
常用标签: <g>标签 是一个容器(分组)标签,用来组合元素的 - 共用属性 - transform = "translate(0,0)"<text>标签 ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- UVA 861 组合数学 递推
题目链接 https://vjudge.net/problem/UVA-861 题意: 一个国际象棋棋盘,‘象’会攻击自己所在位置对角线上的棋子.问n*n的棋盘 摆放k个互相不攻击的 '象' 有多少种 ...