POJ 2400 Supervisor, Supervisee (二分图最大权匹配)

网友投稿 316 2022-08-30

POJ 2400 Supervisor, Supervisee (二分图最大权匹配)

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

271 2 3 4 5 6 72 1 3 4 5 6 73 1 2 4 5 6 74 1 2 3 5 6 75 1 2 3 4 6 76 1 2 3 4 5 77 1 2 3 4 5 61 2 3 4 5 6 72 1 3 4 5 6 73 1 2 4 5 6 74 1 2 3 5 6 75 1 2 3 4 6 76 1 2 3 4 5 77 1 2 3 4 5 621 22 11 21 2

Sample Output

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

题意

有 ​​n​​​ 个老板和 ​​n​​ 个员工,他们对彼此有一个排名,现在要求选出最好的对应关系使他们平均分值最少。

思路

老板和员工可以看作两个不同的集合,然后他们彼此的排名我们可以给其赋一个权值,求二分图的最大权匹配即可。

最少平均分值: km2∗n

根据结果和全排列来寻找老板与员工的匹配,输出~

注意: 题目中输入的两个矩阵好像刚好是颠倒的,反过来输入就对了

AC 代码

#include#include#include#include#includeusing namespace std;#define LL long long#define INF 0x3f3f3fconst int maxn = 20;int g[maxn][maxn];int nx,ny;int linker[maxn],lx[maxn],ly[maxn];int slack[maxn];bool visx[maxn],visy[maxn];int cost,sum;bool mark[maxn];bool DFS(int x){ visx[x]=true; for(int y=0; ytmp) slack[y]=tmp; } return false;}int km(){ memset(linker,-1,sizeof(linker)); memset(ly,0,sizeof(ly)); for(int i=0; ilx[i]) lx[i]=g[i][j]; } for(int x=0; xslack[i]) d=slack[i]; for(int i=0; i=nx) { printf("Best Pairing %d\n",++sum); for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:超级体育大年,腾讯何以成为体育营销“最优解”?(腾讯体育弘扬)
下一篇:地产营销策划做到这种程度,就会成为房企里的核心岗位!(房地产公司营销策划岗位)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~