POJ 2400 Supervisor, Supervisee (maximum right matching of binary graphs) __ graph theory

Source: Internet
Author: User
Description

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


Input

The ' the ' input would contain a single integer greater than 0 specifying the number of test cases.

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

All lines of data in the input file would end with a empty line.


Output

For each test case, write the test Case number (starting and 1) followed by the best average difference written to six di Gits of precision to the "right" of the decimal point. On the "Next line", show which best match it is (starting with 1). On the next N lines, show each supervisor (starting and 1) followed by the employee with which she is matched (1 per Lin e). Note:if There is more than one best match, matches should to listed in ascending the order (sample output).

Separate each of the data set with a 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 The 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 The 2, the, the, and the 3, the-
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 Em  Ployee 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


the

There are n bosses and N employees who have a rank for each other and now ask for the best correspondence to make them have the least score.


train of Thought

Bosses and employees can be seen as two different sets, and then they rank each other we can give it a weight, to find the maximum matching of the two graphs.

min. Average score: km2∗n \frac{km}{2*n}

According to the results and the full arrangement to find the boss and staff matching, output ~

Note: the two matrices entered in the title seem to be just upside down, which in turn is the right one.


AC Code

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <
Iostream> using namespace std;
#define LL Long #define INF 0x3f3f3f const 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; y<ny; 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=0; i<nx; i++) {lx[i]=-inf; for (int j=0; j<ny; J + +) if (G[i][j]>lx[i]) lx[i]=G[I][J];
        for (int x=0; x<nx; x + +) {for (int i=0; i<ny; 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=0; i<ny; i++) if (!visy[i]&&d>slack[i)) d=slack[i];
            for (int i=0; i<nx; i++) if (visx[i)) Lx[i]-=d;
                for (int i=0; i<ny; i++) if (visy[i)) Ly[i]+=d;
        else Slack[i]-=d;
    }} cost=0;
    for (int i=0; i<ny; i++) if (linker[i]!=-1) cost+=g[linker[i]][i];
Return-cost;
    void Dfs (int cap,int x)//Full order search finds all answers {if (x<cost) return;
        if (CAP&GT;=NX) {printf ("Best Pairing%d\n", ++sum); for (int i=0; i<nx; i++) printf ("Supervisor%d with Employee%d\n", i+1,linker[i]+1); else {for (int i=0; i<nx; i++) {if (!mark[i)) {mark[
                I]=true;
                Linker[cap]=i;
                DFS (Cap+1,x+g[cap][i]);
            Mark[i]=false;
    int main () {int T;
    scanf ("%d", &t);
        for (int ti=1; ti<=t; ti++) {scanf ("%d", &nx);
        Ny=nx;
        sum=0;
        memset (G,0,sizeof (g));
        memset (Mark,false,sizeof (Mark));
                for (int i=0; i<nx; i++) for (int j=0; j<ny; J + +) {int x;
                scanf ("%d", &x);
            G[x-1][i]-=j;
                for (int i=0; i<nx; i++) for (int j=0; j<ny; J + +) {int x;
                scanf ("%d", &x);
            G[i][x-1]-=j;
        printf ("Data Set%d, best average difference:%.6f\n", ti,0.5*km ()/nx);
        DFS (0,0);
    printf ("\ n");
}    return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.