lightoj 1011 Marriage Ceremonies (KM模板题)

来源:互联网 发布:纹理过滤 三线性优化 编辑:程序博客网 时间:2024/05/19 02:28

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 collectN 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 nextN lines will contain N integers each. The jth integer in the ith line denotes the priority index between theith man and jth woman. All the integers will be positive and not greater than10000.

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


一道裸的KM模板题,不过网上题解都是状压dp写的,不会状压= =

#include<bits/stdc++.h>#define inf 0x3f3f3f3f#define N 20using namespace std;int vx[N],vy[N],lx[N],ly[N],link[N],mat[N][N];int n;int dfs(int t){    int i;    vx[t]=1;    for(i=1; i<=n; i++)    {        if(vy[i]==0&&lx[t]+ly[i]==mat[t][i])        {            vy[i]=1;            if(link[i]==-1||dfs(link[i]))            {                link[i]=t;                return 1;            }        }    }    return 0;}void KM(){    int i,j,t,k;    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    for(i=1; i<=n; i++)        for(j=1; j<=n; j++)            lx[i]=max(lx[i],mat[i][j]);    for(i=1; i<=n; i++)    {        while(1)        {            memset(vx,0,sizeof(vx));            memset(vy,0,sizeof(vy));            if(dfs(i))                break;            else            {                t=inf;                for(j=1; j<=n; j++)                    if(vx[j])                        for(k=1; k<=n; k++)                            if(vy[k]==0&&lx[j]+ly[k]-mat[j][k]<t)                                t=lx[j]+ly[k]-mat[j][k];                for(j=1; j<=n; j++)                {                    if(vx[j])                        lx[j]-=t;                    if(vy[j])                        ly[j]+=t;                }            }        }    }}int main(){    int t;    scanf("%d",&t);    int cas=1;    while(t--)    {        memset(mat,0,sizeof(mat));        memset(link,-1,sizeof(link));        scanf("%d",&n);        for(int i=1; i<=n; i++)            for(int j=1; j<=n; j++)                scanf("%d",&mat[i][j]);        KM();        int ans=0;        for(int i=1;i<=n;i++)            ans+=mat[link[i]][i];        printf("Case %d: %d\n",cas++,ans);    }    return 0;}