poj1904(二分图最优匹配)

来源:互联网 发布:pdf电子书 知乎 编辑:程序博客网 时间:2024/09/21 06:42

Description

      Once upon a time there lived a king and he had N sons. And the king wanted to marry his beloved sons on the girls that they did love. So one day the king asked his sons to come to his room and tell him whom do they love.

      But the sons of the king were all young men so they could not tell exactly whom they did love. Instead of that they just told him the names of the girls that seemed beautiful to them, but since they were all different, their choices of beautiful girls also did not match exactly.

      The king was wise. He did write down the information that the children have provided him with and called you, his main wizard.

      "I want all my kids to be happy, you know," he told you, "but since it might be impossible, I want at least some of them to marry the girl they like. So please, prepare the marriage list."

     Suddenly you recalled that not so long ago the king told you about each of his sons, so you knew how much he loves him. So you decided to please the king and make such a marriage list that the king would be most happy. You know that the happiness of the king will be proportional to the square root of the sum of the squares of his love to the sons that would marry the girls they like.

      So, go on, make a list to maximize the king's happiness.

Input

      The first line of the input file contains N - the number of king's sons (1 ≤ N ≤ 400). The second line contains N integer numbers Ai ranging from 1 to 1000 - the measures of king's love to each of his sons.

      Next N lines contain lists of king's sons' preferences - first Ki - the number of the girls the i-th son of the king likes, and then Ki integer numbers - the girls he likes (all potentially beautiful girls in the kingdom were numbered from 1 to N, you know, beautiful girls were rare in those days).

Output

      Output N numbers - for each son output the number of the beautiful girl he must marry or 0 if he must not marry the girl he likes.

      Denote the set of sons that marry a girl they like by L, then you must maximize the value of

Sample Input

41 3 2 44 1 2 3 42 1 42 1 42 1 4

Sample Output

2 1 0 4


题意:n个小伙n个姑娘,国王对不同的小伙有不同的爱好程度,会优先给喜欢的小伙进行婚配,现在让你给出具体的方案。

思路:明显的匹配问题,由于仅仅是小伙匹配姑娘,直接KM算法进行最优匹配就好,值得注意的是建图时候的边权值是国王对小伙的喜爱程度。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stack>using namespace std;typedef long long ll;const int maxn=410,inf=1e9;int m,n,tu[410][410],match1[410],match2[410];int KM(){    int s[maxn],t[maxn],l1[maxn],l2[maxn],p,q,ret=0,i,j,k;    ///l1为左边的匹配分量,l2是右边的匹配分量    for(i=0; i<m; i++)    {        for(l1[i]=-inf,j=0; j<n; j++)            l1[i]=tu[i][j]>l1[i]?tu[i][j]:l1[i];        if(l1[i]==-inf)            return -1;    }    for(i=0; i<n; l2[i++]=0);    memset(match1,-1,sizeof(int)*n);    memset(match2,-1,sizeof(int)*n);    for(i=0; i<m; i++)    {        memset(t,-1,sizeof(int)*n);        for(s[p=q=0]=i; p<=q&&match1[i]<0; p++)        {            for(k=s[p],j=0; j<n&&match1[i]<0; j++)                if(l1[k]+l2[j]==tu[k][j]&&t[j]<0)                {                    s[++q]=match2[j],t[j]=k;                    if(s[q]<0)                        for(p=j; p>=0; j=p)                            match2[j]=k=t[j],p=match1[k],match1[k]=j;                }        }        if(match1[i]<0)        {            for(i--,p=inf,k=0; k<=q; k++)                for(j=0; j<n; j++)                    if(t[j]<0&&l1[s[k]]+l2[j]-tu[s[k]][j]<p)                        p=l1[s[k]]+l2[j]-tu[s[k]][j];            for(j=0; j<n; l2[j]+=t[j]<0?0:p,j++);            for(k=0; k<=q; l1[s[k++]]-=p);        }    }    if(!tu[0][match1[0]])        printf("0");    else        printf("%d",match1[0]+1);    for(int i=1; i<n; i++)    {        if(!tu[i][match1[i]])            printf(" 0");        else        printf(" %d",match1[i]+1);    }    puts("");    return ret;}int num[410];int main(){    while(~scanf("%d",&n))    {        memset(tu,0,sizeof(tu));        m=n;        for(int i=0; i<n; i++)            scanf("%d",&num[i]);        for(int i=0; i<n; i++)        {            int q;            scanf("%d",&q);            while(q--)            {                int tt;                scanf("%d",&tt);                tu[i][tt-1]=num[i];            }        }        KM();    }    return 0;}


0 0
原创粉丝点击