B - Marriage Ceremonies
来源:互联网 发布:python 跟踪执行过程 编辑:程序博客网 时间:2024/05/22 18:55
这题用到位运算和状态压缩DP,如:
101011=43;
42表示0,1,3,5四列都已经选择了。
~取反,0取反是1,1取反是0
<<是左移,比如1<<n,表示1往左移n位,即数值大小2的n次方
>>右移,类似左移,数值大小除以2的n次方
&按位与,1与任意数等于任意数本身,0与任意数等于0,即1&x=x,0&x=0
|按位或,x|y中只要有一个1则结果为1
^按位异或,x^y相等则为0,不等则为1
DescriptionYou 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
2
2
1 5
2 1
3
1 2 3
6 5 4
8 1 2
Sample Output
Case 1: 7
101011=43;
42表示0,1,3,5四列都已经选择了。
~取反,0取反是1,1取反是0
<<是左移,比如1<<n,表示1往左移n位,即数值大小2的n次方
>>右移,类似左移,数值大小除以2的n次方
&按位与,1与任意数等于任意数本身,0与任意数等于0,即1&x=x,0&x=0
|按位或,x|y中只要有一个1则结果为1
^按位异或,x^y相等则为0,不等则为1
DescriptionYou 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
2
2
1 5
2 1
3
1 2 3
6 5 4
8 1 2
Sample Output
Case 1: 7
Case 2: 16
#include <cstdio>#include <cstring>#include <algorithm>using namespace std; int dp[17][1<<17]; int a[17][17]; int main() {int n,r;scanf("%d",&r);for(int v=1;v<=r;v++){ scanf("%d",&n); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d",&a[i][j]); for(int i=0;i<n;i++) for(int j=0;j<1<<n;j++) dp[i][j]=-1; //一开始所有的状态都是非法的 for(int i=0;i<n;i++) dp[0][1<<i]=a[0][i]; for(int i=1;i<n;i++) //枚举前i-1行所有已经存在的状态,去更新前i行的状态 for(int j=0;j<(1<<n);j++) if(dp[i-1][j]!=-1)//前i-1行j的状态存在的前提下才能转移 { for(int k=0;k<n;k++) if(!(j & (1<<k))) // k 不在j集合中 dp[i][ j | (1<<k)]=max(dp[i][ j | (1<<k)] ,dp[i-1][j]+a[i][k]); } printf("Case %d: ",v); printf("%d\n",dp[n-1][(1<<n)-1]); }}
- B - Marriage Ceremonies
- Marriage Ceremonies
- lightoj 1011 Marriage Ceremonies
- LightOJ 1011 - Marriage Ceremonies
- LightOJ 1011 Marriage Ceremonies
- 1011 - Marriage Ceremonies
- LightOJ 1011 Marriage Ceremonies
- light 1011 - Marriage Ceremonies
- LightOJ 1011 - Marriage Ceremonies (dp)
- LightOJ1011---Marriage Ceremonies (状压dp)
- lightoj1011 - Marriage Ceremonies【状压dp】
- Marriage Ceremonies--Light OJ 1011
- 1011 - Marriage Ceremonies[状态压缩dp]
- 【DP】 LightOJ 1011 - Marriage Ceremonies状压
- Light 1011 - Marriage Ceremonies (费用流)
- LightOJ 1011 Marriage Ceremonies【状压DP】
- light oj 1011 Marriage Ceremonies(状压DP)
- LightOJ 1011 - Marriage Ceremonies (状压dp)
- Eclipse生成可执行的jar包方法
- 关于python文件操作
- WebKit渲染
- 适配器模式入门
- Find min K
- B - Marriage Ceremonies
- Maven中,添加不上oracle 的JDBC的解决办法
- UNIX学习笔记-文件I/O--(creat,close)
- Linux 命令搜集
- 成从武:大数据是地图领域的核心竞争力
- 设计模式汇总
- java实现读出Properties文件
- 小电池和小屏幕手机网站设计
- Xcode 4-PBXcp error修复-No such file or directory