ACdream 1227 Beloved 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

题目分析:

题目大意是有国王n个王子,有n个姑娘,(是不是很熟悉?)不同的是国王对每一个儿子的喜爱程度不一样。要求你对王子和姑娘进行配对,使得国王的开心度最高。(每配对一个王子,国王获得其喜爱度值的开心度)
很明显的二分图权匹配问题,在每个王子与其喜欢的姑娘之间的边权上附上国王的喜爱度。用km算法跑一边就可以了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

const int MAXN=500;
const int INF=0x3f3f3f3f;
using namespace std;


int n;
int w[MAXN];
int G[MAXN][MAXN];
int linker[MAXN],lx[MAXN],ly[MAXN];
int slack[MAXN];
bool visx[MAXN],visy[MAXN];

bool dfs(int x)
{
    visx[x]=true;
    for(int y=1; y<=n; y++)
    {
        if (visy[y]) continue;
        int tmp=lx[x]+ly[y]-G[x][y];
        if (tmp==0)
        {
            visy[y]=true;
            if (linker[y]==-1 || dfs(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if (slack[y]>tmp) slack[y]=tmp;
    }
    return false;
}

int km()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(int i=1; i<=n; i++)
    {
        lx[i]=-INF;
        for(int j=1; j<=n; j++)
        {
            if (G[i][j]>lx[i])
                lx[i]=G[i][j];
        }
    }
    for(int x=1; x<=n; x++)
    {
        for(int i=1; i<=n; i++) slack[i]=INF;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if (dfs(x)) break;
            int d=INF;
            for(int  i=1; i<=n; i++)
                if (!visy[i] && d>slack[i]) d=slack[i];
            for(int i=1; i<=n; i++)
                if (visx[i]) lx[i]-=d;
            for(int i=1; i<=n; i++)
            {
                if (visy[i]) ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    int res=0;
    for(int i=1; i<=n; i++)
        if (linker[i]!=-1)
            res+=G[linker[i]][i];
    return res;
}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        for(int i=1; i<=n; i++)
            scanf("%d",&w[i]);
        memset(G,0,sizeof(G));
        for(int i=1; i<=n; i++)
        {
            int t;
            scanf("%d",&t);
            while(t--)
            {
                int v;
                scanf("%d",&v);
                G[i][v]=w[i];
            }
        }
        km();
        int ans[MAXN];
        memset(ans,0,sizeof(ans));
        for(int i=1; i<=n; i++)
        {
            if(G[linker[i]][i]) ans[linker[i]]=i;
        }
        for(int i=1; i<=n; i++)
        {
            if (i<n) printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值