ACdream 1227Beloved Sons【二分图最佳匹配】

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

4
1 3 2 4
4 1 2 3 4
2 1 4
2 1 4
2 1 4

Sample Output

2 1 0 4

错过了下午的比赛,学弟说这个题他们默认我会图论,就贴了模板过的,心虚晚上回来补题,50min1A(最快的队伍都是1个小时出头才出的,哼哼)

题意:依旧是给王子们选心爱的女孩,给出男孩子们分别喜欢的女生,以及国王对于每个王子的偏爱值,最终的结果是得到自己喜欢女孩子的王子的偏爱值的平方和,要求这个值最大。输出每个男孩子对应选择女生的标号。

做法:从模板可以看出link[j]=i表示女生j与王子i配对,那么如果配对的不是王子喜欢的输出0

    #include <stdio.h>
    #include <string.h>
    #define M 410
    #define inf 0x3f3f3f3f
    using namespace std;
    int abs(int x)
    {
        return x>0?x:-x;
    }
    int n,m,nx,ny;
    int link[M],lx[M],ly[M],slack[M];///lx,ly为顶标,nx,ny分别为x点集y点集的个数
    int visx[M],visy[M],w[M][M];

    int DFS(int x)
    {
        visx[x] = 1;
        for (int y = 1; y <= ny; y ++)
        {
            if (visy[y]) continue;
            int t = lx[x] + ly[y] - w[x][y];
            if (t == 0)
            {
                visy[y] = 1;
                if (link[y] == -1||DFS(link[y]))
                {
                    link[y] = x;
                    return 1;
                }
            }
            else if (slack[y] > t)  ///不在相等子图中slack 取最小的
                slack[y] = t;
        }
        return 0;
    }
    int KM()
    {
        int i,j;
        memset (link,-1,sizeof(link));
        memset (ly,0,sizeof(ly));
        for (i = 1; i <= nx; i ++)          ///lx初始化为与它关联边中最大的
            for (j = 1,lx[i] = -inf; j <= ny; j ++)
                if (w[i][j] > lx[i])
                    lx[i] = w[i][j];

        for (int x = 1; x <= nx; x ++)
        {
            for (i = 1; i <= ny; i ++)
                slack[i] = inf;
            while (1)
            {
                memset (visx,0,sizeof(visx));
                memset (visy,0,sizeof(visy));
                if (DFS(x))     ///若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
                    break;  ///若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
                ///方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
                ///所有在增广轨中的Y方点的标号全部加上一个常数d
                int d = inf;
                for (i = 1; i <= ny; i ++)
                    if (!visy[i]&&d > slack[i])
                        d = slack[i];
                for (i = 1; i <= nx; i ++)
                    if (visx[i])
                        lx[i] -= d;
                for (i = 1; i <= ny; i ++) ///修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
                    if (visy[i])
                        ly[i] += d;
                    else
                        slack[i] -= d;
            }
        }
        int res = 0;
        int tmp[M];
        for (i = 1; i <= ny; i ++)
        {
            if (link[i] > -1)
            {
                for(int j=1;j<=nx;j++)
                {
                    if(link[i]==j)
                    {
                        if(w[link[i]][i]!=0)
                            tmp[j]=i;
                        else tmp[j]=0;
                        break;
                    }
                }
                res += w[link[i]][i];
            }
         //   else printf("0 ");
        }
        for(int i=1;i<nx;i++)printf("%d ",tmp[i]);
        printf("%d\n",tmp[nx]);
        //puts("");
     //   printf("res=%d\n",res);
        return res;
    }
    int cost[M];
    int main()
    {
      //  freopen("cin.txt","r",stdin);
        int n;
        while(~scanf("%d",&n))
        {
            nx=ny=n;
            memset(w,0,sizeof(w));
            for(int i=1;i<=n;i++)scanf("%d",&cost[i]);
            for(int i=1;i<=n;i++)
            {
                int k,a;
                scanf("%d",&k);
                while(k--)
                {
                    scanf("%d",&a);
                    w[i][a]=cost[i]*cost[i];
                }
            }
            KM();
        }
        return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值