lightoj 1011 - Marriage Ceremonies(状态压缩dp)
来源:互联网 发布:http 411 nginx 编辑:程序博客网 时间:2024/05/17 08:37
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: 16
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个表,第i,j个表示第i个男人和第j个女人匹配可以得到的价值,每个人只能匹配一个异性,求最大价值和。
思路:n的范围只有16,可以用状态压缩,dp[i][j]表示前i个男人匹配状态为j的最大价值和,于是又状态转移 dp[i][j|temp] = max(dp[i][j|temp], dp[i-1][j] + Map[i][k])。
#include<iostream>#include<cstdio>#include<algorithm>#include<map>#include<cstring>#include<map>#include<set>#include<queue>#include<stack>#include<cmath>using namespace std;const double eps = 1e-10;const int inf = 0x3f3f3f3f, N = 2e1 +10;int Map[N][N];int dp[N][(1<<16)+10];int main(){ int n, t; cin>>t; for(int cas = 1; cas<=t; cas++) { scanf("%d", &n); for(int i = 0; i<n; i++) for(int j = 0; j<n; j++) scanf("%d",&Map[i][j]); memset(dp, 0, sizeof(dp)); int temp, res = 1<<n; for(int i = 0; i<n; i++) dp[0][1<<i] = Map[0][i]; for(int i = 1; i<n; i++) { for(int j = 0; j<res; j++) for(int k = 0; k<n; k++) { temp = 1<<k; if(j & temp) continue; dp[i][j|temp] = max(dp[i][j|temp], dp[i-1][j] + Map[i][k]); } } printf("Case %d: %d\n", cas, dp[n-1][res-1]); } return 0;}
- lightoj 1011 - Marriage Ceremonies(状态压缩dp)
- LightOJ-1011-Marriage Ceremonies [状态压缩][DP]
- lightoj 1011 Marriage Ceremonies (状态压缩dp~)
- 1011 - Marriage Ceremonies[状态压缩dp]
- LightOJ 1011 - Marriage Ceremonies (dp)
- Light OJ 1011 Marriage Ceremonies 状态压缩DP
- 【DP】 LightOJ 1011 - Marriage Ceremonies状压
- LightOJ 1011 Marriage Ceremonies【状压DP】
- LightOJ 1011 - Marriage Ceremonies (状压dp)
- LightOJ - 1011 Marriage Ceremonies(状压DP)
- LightOJ 1011 - Marriage Ceremonies(dp)
- LightOJ 1011 - Marriage Ceremonies(状压DP)
- lightoj 1011 Marriage Ceremonies (状压dp)
- lightoj 1011 Marriage Ceremonies
- LightOJ 1011 - Marriage Ceremonies
- LightOJ 1011 Marriage Ceremonies
- LightOJ 1011 Marriage Ceremonies
- LightOJ 1011 Marriage Ceremonies (二分图最优匹配 状态压缩+记忆化搜索)
- jquery ui widget原理解析
- 快速排序简单讲解
- ListView的异步加载图片问题与优化
- Spring的jar包描述
- JAVA中String的常用方法?
- lightoj 1011 - Marriage Ceremonies(状态压缩dp)
- JS判断元素类型
- js日期处理---计算自然月、指定自然月某一天、获取前一天和后一天、判断闰年、当月最后一天日期、月末最后一天
- 使用Struts实现防止表单重复提交
- 输出数中最小的一个数在最后!!!
- Windows Dev Intro - VS2013 Compile X64 Project Upgraded from Win32 Project
- 静态变量static学习
- (源码分析)Android-Universal-Image-Loader (图片异步加载缓存库)的使用配置
- iOS开发,将子线程获取的数据实时更新到UI