1011-marriage Ceremonies
PDF (中文版) statisticsforum
Time Limit:2 second (s) Memory limit:32 MB
which organizes marriages. Marriages is isn't the easy-to-be-made, so, the job was quite hard for you.
The job gets more difficult if 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 are in a danger and you want to save your company from this financial crisis by arranging as much marriage s 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's total priority index is maximized. Remember that all man should is paired with a woman and only monogamous families should is formed.
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case contains a integer N (1≤n≤16), denoting the number of men or women. Each of the next n lines would 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 is positive and not greater than 10000.
Output
For each case, print the case number and the maximum possible, the marriages has 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-solving ideas: using Dp[i][j] to express, where I is the calculation to the first layer, J of the binary 1 means that the number of those positions have been taken
State transition Equation dp[i][j| ( 1<<k)] = max (dp[i][j],dp[i-1][j],map[i-1][k]);
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd;intmap[ -][ -];intdp[ -][65635];intT,n,len;intMain () {scanf ("%d",&T); for(intt=1; t<=t;t++) {memset (DP,0,sizeof(DP)); scanf ("%d",&N); Len= (int) Pow (2, N); for(intI=0; i<n;i++) for(intj=0; j<n;j++) scanf ("%d",&Map[i][j]); for(intI=1; i<=n;i++){ for(intj=0; j<n;j++){ for(intk=0; k<len;k++){ if(((1<<J) &k))Continue; dp[i][(1<<J) |k] = max (dp[i][(1<<J) |k],dp[i-1][k]+map[i-1][j]); }}} printf ("Case %d:%d\n", t,dp[n][len-1]); } return 0;}
Lightoj-1011-marriage ceremonies (pressure DP)