"POJ 2400" Supervisor, supervisee (km minimum weight matching)

Source: Internet
Author: User
Tags bool min

"POJ 2400" Supervisor, supervisee (km minimum weight matching)


Supervisor, Supervisee

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2538 Accepted: 719

Description Suppose some supervisors each get to hire a new person for their department. There is n people to being placed in these N departments. Each supervisor interviews all N people, and ranks them according to how much she wants each of the 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-much that person would like to work for that SUP Ervisor (again, 1 is "really want-to-work for him/her" and N-is "really don ' t want-work for him/her"). Given the scores that each supervisor have for each candidate, and the scores each candidate have for each manager, write a Computer program to determine the ' best match ' of candidates to supervisors. The "best match" was determined by finding the distribution, leads to the 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 would be 0.

Input the first line of 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 number of Employees-there is N supervisors and N employees). The next N lines'll is 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 this supervisor from most preferred to least preferred. More specifically, the first entry on the line would represent that supervisor's first choice, the second entry her 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 the data in the input file would end with a 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 is (starting with 1). On the next N lines, show all supervisor (starting with 1) followed by the employee with which she was matched (1 per Lin e). Note:if There is more than one best match, matches should being listed in ascending permuted order (see sample Output).

Separate each 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 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 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

Source Tehran 2003 Preliminary


The main idea is that n bosses and N employees. Every boss has an employee who wants to match, and every employee has a boss who is eager to match.

Enter first behavior n after n behavior 1~n boss's expectations from left to right from best to worst

Again, the N line is the 1~n employee.

Match to the most desired person value is 0, otherwise from left to right once plus 1

Ask for the average expected minimum value, which is the minimum value/2n

Minimum value in km minimum weight matching calculation can be, because the output solution is also required, there are many solutions to the output of multiple solutions. So we have to search ...

In fact, few data, find the smallest right match also use search also can ...


The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <cstring> #include <queue> #include <list> #include <algorithm> #include <map&
Gt #include <set> #define LL Long Long #define FREAD () freopen ("In.in", "R", stdin) #define FWRITE () freopen ("Out.out", "
W ", stdout) using namespace std;
const int INF = 0X3F3F3F3F;
const int MSZ = 10000;

Const double EPS = 1e-8;
int mp[23][33];
int lx[33],ly[33],link[33],slack[33],next[33];
BOOL Visx[33],visy[33],vis[33];

int n,ans,cnt;

	BOOL cal (int x) {visx[x] = 1;
		
		for (int y = 0; y < n; ++y) {if (visy[y]) continue;
		int t = Lx[x]+ly[y]-mp[x][y];
			if (t = = 0) {Visy[y] = 1;
				if (link[y] = = 1 | | cal (Link[y])) {link[y] = x;
			return 1;
	}} else Slack[y] = min (slack[y],t);
} return 0;

	} int KM (memset) {link,-1,sizeof (link));
		for (int i = 0; i < n; ++i) {memset (slack,inf,sizeof (slack));
		while (1){memset (visx,0,sizeof (VISX));

			memset (visy,0,sizeof (Visy));

			if (Cal (i)) break;
			int d = INF;

			for (int i = 0; i < n; ++i) if (!visy[i]) d = min (D,slack[i]);

			for (int i = 0; i < n; ++i) if (Visx[i]) lx[i]-= D;
				for (int i = 0; i < n; ++i) if (Visy[i]) ly[i] + = D;
		else Slack[i]-= D;
	}} ans = 0;

	for (int i = 0; i < n; ++i) if (link[i]! =-1) ans + = mp[link[i]][i];
Return-ans;
	} void Dfs (int id,int HS) {if (HS < ans) return;
			if (id = = N) {if (hs = = ans) {printf ("Best Pairing%d\n", ++cnt);
			for (int i = 0; i < n; ++i) {printf ("Supervisor%d with Employee%d\n", i+1,next[i]+1);
	}} return;
		} for (int i = 0; i < n; ++i) {if (vis[i]) continue;
		Vis[i] = 1;
		Next[id] = i;
		DFS (Id+1,hs+mp[id][i]);
	Vis[i] = 0;
	}} int main () {int t,x;
	
	scanf ("%d", &t);
		for (int z = 1; z <= t; ++z) {scanf ("%d", &n);

	
		memset (ly,0,sizeof (ly)); for (int i = 0, i < n; ++i) for (int j = 0; j< n;
				++J) {scanf ("%d", &x);
			Mp[x-1][i] =-j;
				} for (int i = 0; i < n; ++i) for (int j = 0; J < N; ++j) {scanf ("%d", &x);
				Mp[i][x-1]-= j;
				if (j = = 0) Lx[i] = mp[i][x-1];
			else Lx[i] = max (lx[i],mp[i][x-1]);

		} printf ("Data Set%d, best average difference:%.6f\n", z,km () *0.5/n);
		CNT = 0;
		memset (vis,0,sizeof (VIS));
		DFS (0,0);
	Puts ("");
} 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.