poj2400 Supervisor, Supervisee 二分图匹配

来源:互联网 发布:手机ar软件 编辑:程序博客网 时间:2024/06/03 22:40



Supervisor, Supervisee
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2820 Accepted: 792

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名员工。每个上司对应有想要搭配的员工,同样每个员工有渴望搭配的上司。

这个题要求输出最佳匹配下的所有界的个数,用一次搜索就可以了

#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>#include<set>#include<stack>using namespace std;const int maxn=20;const int inf=99999999;int n,sum,cost;int link[maxn];bool useds[maxn],usede[maxn];int line[maxn],preline[maxn];int w[maxn][maxn];int s[maxn][maxn],e[maxn][maxn];int cs[maxn],ce[maxn];bool book[maxn];bool find(int x){usede[x]=true;for(int i=1;i<=n;i++){if(useds[i]==false&&(ce[x]+cs[i]==w[x][i])){useds[i]=true;if(line[i]==0||find(line[i])){line[i]=x;return true;}}}return false;}int km(){for(int i=1;i<=n;i++){while(true){memset(useds,false,sizeof(useds));memset(usede,false,sizeof(usede));int d=inf;if(find(i))break;for(int j=1;j<=n;j++){if(usede[j]){for(int k=1;k<=n;k++){if(useds[k]==0){d=min(d,ce[j]+cs[k]-w[j][k]);}}}}if(d==inf)return -1;for(int j=1;j<=n;j++){if(usede[j]){ce[j]-=d;}}for(int j=1;j<=n;j++){if(useds[j]){cs[j]+=d;}}}}double ans=0;for(int i=1;i<=n;i++){ans+=w[line[i]][i];}cost=ans;ans=0-ans;ans-=2*n;printf("Best average difference: %.6lf\n",ans*1.0/(n*2));// printf("Best Pairing 1\n");// for(int i=1;i<=n;i++){// printf("Supervisor %d with Employee %d\n",i,line[i]);// }return ans;}void dfs(int cur,int cnt){if(cnt<cost){return;}if(cur==n+1){if(cnt!=cost){return;}sum++;printf("Best Pairing %d\n",sum);for(int i=1;i<=n;i++){printf("Supervisor %d with Employee %d\n",i,link[i]);}}else{for(int i=1;i<=n;i++){if(book[i]==false){book[i]=true;link[cur]=i;dfs(cur+1,cnt+w[cur][i]);book[i]=false;}}}}int main(){//freopen("test.txt","r",stdin);int ca=0;int t;scanf("%d",&t);int i,j;while(t--){scanf("%d",&n);memset(w,0,sizeof(w));memset(line,0,sizeof(line));memset(preline,0,sizeof(line));memset(book,false,sizeof(book));sum=0;cost=0;for(i=1;i<=n;i++){for(j=1;j<=n;j++){scanf("%d",&s[i][j]);w[s[i][j]][i]+=j;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++){scanf("%d",&e[i][j]);w[i][e[i][j]]+=j;}}// for(i=1;i<=n;i++){// for(j=1;j<=n;j++){// printf("%d ",w[i][j]);// }// printf("\n");// }memset(ce,0,sizeof(ce));memset(cs,0,sizeof(cs));for(i=1;i<=n;i++){int d=-inf;for(j=1;j<=n;j++){w[i][j]=0-w[i][j];d=max(d,w[i][j]);}ce[i]=d;}printf("Data Set %d, ",++ca);km();dfs(1,0);printf("\n");}return 0;}



原创粉丝点击