poj1904(二分图最优匹配)

202 篇文章 0 订阅
123 篇文章 0 订阅

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 <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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值