poj 2400 Supervisor, Supervisee

Supervisor, Supervisee
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2732 Accepted: 765

Description

Suppose some supervisors each get to hire a new person for their department. There are N people to be placed in these N departments. Each supervisor interviews all N people, and ranks them according to how much she wants each of them in her department (1 being "really want" and N being "really don't want"). In turn, each of the N candidates ranks each of the supervisors as to how much that person would like to work for that supervisor (again, 1 is "really want to work for him/her" and N is "really don't want to work for him/her"). Given the scores that each supervisor has for each candidate, and the scores each candidate has for each manager, write a computer program to determine the "best match" of candidates to supervisors. The "best match" is determined by finding the distribution that leads to the highest overall (i.e. sum of) satisfaction for all people. The closer a person is to her number one choice, the better. If everyone gets their number one choice, the average difference will be 0.

Input

The first line of the input will contain a single integer greater than 0 specifying the number of test cases. 

The next line will contain a single integer value N, 0 < N < 15, representing the number of supervisors (and the number of employees - there are N supervisors and N employees). The next N lines will be the preferences of each of the N supervisors. Each line will contain N integer entries (1 through N for employees 1 through N), each separated by a space character, that represents the preferences of that supervisor from most preferred to least preferred. More specifically, the first entry on the line will represent that supervisor's first choice, the second entry her second, and so on. The next N lines will be the preferences of the N employees, in the same format as the supervisors. 

All lines of data in the input file will end with an empty line.

Output

For each test case, write the test case number (starting with 1) followed by the best average difference written to six digits of precision to the right of the decimal point. On the next line, show which best match it was (starting with 1). On the next N lines, show each supervisor (starting with 1) followed by the employee with which she was matched (1 per line). NOTE: if there is more than one best match, matches should be listed in ascending permuted order (see sample output). 

Separate each data set with an empty line.

Sample Input

2
7
1 2 3 4 5 6 7
2 1 3 4 5 6 7
3 1 2 4 5 6 7
4 1 2 3 5 6 7
5 1 2 3 4 6 7
6 1 2 3 4 5 7
7 1 2 3 4 5 6
1 2 3 4 5 6 7
2 1 3 4 5 6 7
3 1 2 4 5 6 7
4 1 2 3 5 6 7
5 1 2 3 4 6 7
6 1 2 3 4 5 7
7 1 2 3 4 5 6

2
1 2
2 1
1 2
1 2

Sample Output

Data Set 1, Best average difference: 0.000000
Best Pairing 1
Supervisor 1 with Employee 1
Supervisor 2 with Employee 2
Supervisor 3 with Employee 3
Supervisor 4 with Employee 4
Supervisor 5 with Employee 5
Supervisor 6 with Employee 6
Supervisor 7 with Employee 7

Data Set 2, Best average difference: 0.250000
Best Pairing 1
Supervisor 1 with Employee 1
Supervisor 2 with Employee 2

Source

提示

题意:

有n(0<n<15)个老板和n个员工,下面有2*n行,第一个n行给出每个员工对第x个老板的讨厌度(从小到大),第二个n行给出每个老板对第x个员工的讨厌度(从小到大,题目描述的矩阵上面是老板对员工,下面是员工对老板)。求出(∑(两人讨厌度和-2))/(2*n)最小,并且当前是第几种情况以及输出配对方式,会有多组,按字典序来排。

思路:

二分图最优匹配,KM是求最大匹配,所以要转化为负数形式,才能求出最小。

最优匹配只是求出最小值,还需要用一下dfs搜一下有几种情况,反正n很小随便了。

示例程序

Source Code

Problem: 2400		Code Length: 2949B
Memory: 380K		Time: 79MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
#define MIN -1e9+7
int map[15][15],match[15],lx[15],ly[15],sx[15],sy[15],v[15],num,ans;
int path(int pos,int n)
{
    int i;
    sx[pos]=1;
    for(i=0;n>i;i++)
    {
        if(sy[i]==0&&lx[pos]+ly[i]==map[pos][i])
        {
            sy[i]=1;
            if(match[i]==-1||path(match[i],n)==1)
            {
                match[i]=pos;
                return 1;
            }
        }
    }
    return 0;
}
int km(int n)
{
    int i,i1,i2,max,sum=0;
    for(i=0;n>i;i++)
    {
        lx[i]=MIN;
        ly[i]=0;
        for(i1=0;n>i1;i1++)
        {
            if(lx[i]<map[i][i1])
            {
                lx[i]=map[i][i1];
            }
        }
    }
    memset(match,-1,sizeof(match));
    for(i=0;n>i;i++)
    {
        while(1)
        {
            memset(sx,0,sizeof(sx));
            memset(sy,0,sizeof(sy));
            if(path(i,n)==1)
            {
                break;
            }
            max=-MIN;
            for(i1=0;n>i1;i1++)
            {
                if(sx[i1]==1)
                {
                    for(i2=0;n>i2;i2++)
                    {
                        if(sy[i2]==0&&max>lx[i1]+ly[i2]-map[i1][i2])
                        {
                            max=lx[i1]+ly[i2]-map[i1][i2];
                        }
                    }
                }
            }
            for(i1=0;n>i1;i1++)
            {
                if(sx[i1]==1)
                {
                    lx[i1]=lx[i1]-max;
                }
                if(sy[i1]==1)
                {
                    ly[i1]=ly[i1]+max;
                }
            }
        }
    }
    for(i=0;n>i;i++)
    {
        sum=sum-map[match[i]][i];
    }
    return sum;
}
void dfs(int pos,int t,int n)
{
    int i;
    if(t>ans)				//超过ans的也就不可能了
    {
        return;
    }
    if(pos==n)
    {
        if(t==ans)
        {
            printf("Best Pairing %d\n",num);
            num++;
            for(i=0;n>i;i++)
            {
                printf("Supervisor %d with Employee %d\n",i+1,match[i]+1);
            }
        }
        return;
    }
    for(i=0;n>i;i++)
    {
        if(v[i]==0)
        {
            v[i]=1;
            match[pos]=i;
            dfs(pos+1,t-map[pos][i],n);
            v[i]=0;
        }
    }
}
int main()
{
    int t,i,i1,i2,n,x;
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        scanf("%d",&n);
        for(i1=0;n>i1;i1++)
        {
            for(i2=0;n>i2;i2++)
            {
                scanf("%d",&x);
                map[x-1][i1]=-i2;			//员工对老板
            }
        }
        for(i1=0;n>i1;i1++)
        {
            for(i2=0;n>i2;i2++)
            {
                scanf("%d",&x);
                map[i1][x-1]=map[i1][x-1]-i2;			//老板对员工
            }
        }
        ans=km(n);
        printf("Data Set %d, Best average difference: %f\n",i,ans*0.5/n);
        memset(v,0,sizeof(v));
        num=1;
        dfs(0,0,n);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值