lightoj 1011 Marriage Ceremonies (状压dp)
来源:互联网 发布:exceed2008是什么软件 编辑:程序博客网 时间:2024/05/19 05:06
Marriage Ceremonies
Time Limit: 2 second(s) Memory Limit: 32 MB
Time Limit: 2 second(s) Memory Limit: 32 MB
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
22
1 5
2 1
3
1 2 3
6 5 4
8 1 2
Case 1: 7
Case 2: 16
题目链接:http://lightoj.com/volume_showproblem.php?problem=1011
题目大意:一个n*n的矩阵,不能取同行或同列,求最大总和
题目分析:二分图最大权匹配的模板题,看到数据范围,尝试用状压dp来做,设dp[i][sta]表示到第i行,列选择状态为sta时的最大值,转移很简单,直接看列上是否冲突即可,时间复杂度n^2 * 2^n
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 17;int n, a[MAX][MAX];int dp[MAX][1 << MAX];int main() { int T; scanf("%d", &T); for(int ca = 1; ca <= T; ca ++) { printf("Case %d: ", ca); memset(dp, 0, sizeof(dp)); scanf("%d", &n); for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { scanf("%d", &a[i][j]); dp[i][1 << j] = a[i][j]; } } for(int sta = 0; sta < (1 << n); sta ++) { for(int i = 1; i < n; i ++) { if(dp[i - 1][sta]) { for(int j = 0; j < n; j ++) { if(!(sta & (1 << j))) { dp[i][sta | (1 << j)] = max(dp[i][sta | (1 << j)], dp[i - 1][sta] + a[i][j]); } } } } } printf("%d\n", dp[n - 1][(1 << n) - 1]); }}
0 0
- 【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 【状压dp or KM】
- lightoj 1011 - Marriage Ceremonies 详解(状压DP入门题)
- Lightoj 1011 Marriage Ceremonies(状压dp入门 or km板子)
- 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 (状态压缩dp~)
- 好题集锦
- 100天土鸡饲养计划(38)
- Java台球游戏2
- tomcat中reloadable作用
- 内核管理之CPU基本组件
- lightoj 1011 Marriage Ceremonies (状压dp)
- SDL初步学习
- 【Codeforces Round #366 (Div. 2)】Codeforces 705B Spider Man
- os引导程序boot从扇区拷贝os加载程序loader文件到内存(boot copy kernel to mem in the same method)
- UVA 10006(素数判断+快速幂)
- Volley源码分析
- 学习DWZ框架小结
- monkeyrunner自动化测试
- 【web前端技术】八款JS(javascript)常用开发框架介绍及比较(web前端开发框架)