light oj 1011 一个普通记录...(路径?)的状压dp

来源:互联网 发布:wifislax秒破wpa2网络 编辑:程序博客网 时间:2024/04/23 23:58

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

这个题看见n<=16基本上就可以往状压上面想了…

dp数组存的是每个男生的不同状态
p这个结构体里边存的是每个女生是否结婚的状态
然后每当状态转移的时候连哪个女生是否婚配一起转移…
新加上的那个就标记好…

#include <iostream>#include<cstdio>#include<vector>#include<algorithm>#include<cmath>#include<memory.h>using namespace std;int zhi[17][17],n,u=0;struct p{    int zhi;    bool shi[17];};p dp[1<<17];int main(){    int T;    cin>>T;    while(T--)    {        cin>>n;        memset(zhi,0,sizeof(zhi));        memset(dp,0,sizeof(dp));        for(int a=1;a<=n;a++)for(int b=1;b<=n;b++)scanf("%d",&zhi[a][b]);        int quan=1<<n;        for(int a=1;a<quan;a++)        {            for(int b=n-1;b>=0;b--)            {                int ce=1<<b;                if(ce&a)                {                    int qian=a-ce;                    int maxx=0;                    int weizhi;                    for(int c=1;c<=n;c++)                    {                        if(dp[qian].shi[c]==0)                        {                            if(maxx<zhi[b+1][c])                            {                                maxx=zhi[b+1][c];                                weizhi=c;                            }                        }                    }                    p q=dp[qian];                    if(dp[a].zhi<q.zhi+maxx)                    {                        q.shi[weizhi]=1;                        q.zhi+=maxx;                        dp[a]=q;                    }                }            }        }        printf("Case %d: %d\n",++u,dp[quan-1].zhi);    }    return 0;}
0 0
原创粉丝点击